最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

非常簡單的Android打開和保存對話框功能

 更新時間:2016年07月15日 10:29:06   投稿:lijiao  
這篇文章主要介紹了非常簡單的Android打開和保存對話框功能,感興趣的小伙伴們可以參考一下

在Android上沒有標準的打開和另存為對話框。在本代碼中,我將詳細描述一個非常簡單的打開和保存對話框?qū)崿F(xiàn)過程,對于Android初學者來說非常有用,對話框都是全屏活動的。

主要功能:

1、訪問任何目錄的SD卡
2、遞歸訪問文件夾
3、單一文件選擇
4、通過按硬件后退按鈕升級
5、確認文件選擇OK按鈕
 

activity_open_file.xml

<LinearLayout xmlns:android="<a  rel="nofollow" target="_blank">http://schemas.android.com/apk/res/android"</a>
  xmlns:tools="<a  rel="nofollow" target="_blank">http://schemas.android.com/tools"</a>
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

  <ListView
    android:id="@+id/LvList"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1" >

  </ListView>
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
      android:id="@+id/BtnOK"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="OK" />
    <Button
      android:id="@+id/BtnCancel"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="Cancel" />

  </LinearLayout>
</LinearLayout>

OpenFileActivity.java

package com.example.androidfiledialogs;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;


public class OpenFileActivity extends Activity
  implements OnClickListener, OnItemClickListener {
  ListView LvList;
  ArrayList<String> listItems = new ArrayList<String>();
  ArrayAdapter<String> adapter;
  Button BtnOK;
  Button BtnCancel;
  String currentPath = null;
  String selectedFilePath = null; /* Full path, i.e. /mnt/sdcard/folder/file.txt */
  String selectedFileName = null; /* File Name Only, i.e file.txt */
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_open_file);
    try {

      /* Initializing Widgets */
      LvList = (ListView) findViewById(R.id.LvList);
      BtnOK = (Button) findViewById(R.id.BtnOK);
      BtnCancel = (Button) findViewById(R.id.BtnCancel);    

      /* Initializing Event Handlers */
      LvList.setOnItemClickListener(this);
      BtnOK.setOnClickListener(this);
      BtnCancel.setOnClickListener(this);

      //
    setCurrentPath(Environment.getExternalStorageDirectory().getAbsolutePath() + "/");
    } catch (Exception ex) {
      Toast.makeText(this,
          "Error in OpenFileActivity.onCreate: " + ex.getMessage(),
          Toast.LENGTH_SHORT).show();
    }

  }

  void setCurrentPath(String path) {
    ArrayList<String> folders = new ArrayList<String>();
    ArrayList<String> files = new ArrayList<String>();
    currentPath = path;
    File allEntries = new File(path).listFiles();
    for (int i = 0; i < allEntries.length; i++) {
      if (allEntries.isDirectory()) {
        folders.add(allEntries.getName());
      } else if (allEntries.isFile()) {
        files.add(allEntries.getName());
      }
    }

    Collections.sort(folders, new Comparator<String>() {
      @Override
      public int compare(String s1, String s2) {
        return s1.compareToIgnoreCase(s2);
      }
    });

     Collections.sort(files, new Comparator<String>() {
      @Override
      public int compare(String s1, String s2) {
        return s1.compareToIgnoreCase(s2);

      }

    });

    listItems.clear();
    for (int i = 0; i < folders.size(); i++) {
      listItems.add(folders.get(i) + "/");
    }

    for (int i = 0; i < files.size(); i++) {
      listItems.add(files.get(i));

    }

    

    adapter = new ArrayAdapter<String>(this,

        android.R.layout.simple_list_item_1,

        listItems);

    adapter.notifyDataSetChanged();

    

    LvList.setAdapter(adapter);

  }

  

  @Override

  public void onBackPressed()

  {

    if (!currentPath.equals(Environment.getExternalStorageDirectory().getAbsolutePath() + "/")) {

      setCurrentPath(new File(currentPath).getParent() + "/");

    } else {

      super.onBackPressed();

    }

  }

  

  @Override

  public void onClick(View v) {

    Intent intent;

    

    switch (v.getId()) {

    case R.id.BtnOK:

      

      intent = new Intent();

      intent.putExtra("fileName", selectedFilePath);

      intent.putExtra("shortFileName", selectedFileName);

      setResult(RESULT_OK, intent);

      

      this.finish();

      

      break;

    case R.id.BtnCancel:

      

      intent = new Intent();

      intent.putExtra("fileName", "");

      intent.putExtra("shortFileName", "");

      setResult(RESULT_CANCELED, intent);

      

      this.finish();

      

      break;

    }

  }


  @Override

  public void onItemClick(AdapterView<?> parent, View view, int position,

      long id) {

    String entryName = (String)parent.getItemAtPosition(position);

    if (entryName.endsWith("/")) {

      setCurrentPath(currentPath + entryName);

    } else {

      selectedFilePath = currentPath + entryName;

      

      selectedFileName = entryName;

      

      this.setTitle(this.getResources().getString(R.string.title_activity_open_file)

          + "<span>[</span>" + entryName + "]");

    }

  }

}

activity_save_file.xml

<LinearLayout xmlns:android="<a  rel="nofollow" target="_blank">http://schemas.android.com/apk/res/android"</a>

  xmlns:tools="<a  rel="nofollow" target="_blank">http://schemas.android.com/tools"</a>

  android:layout_width="match_parent"

  android:layout_height="match_parent"

  android:orientation="vertical" >


  <ListView

    android:id="@+id/SFA_LvList"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:layout_weight="1" >

  </ListView>


  <EditText

    android:id="@+id/SFA_TxtFileName"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:ems="10"

    android:text="file.txt" />

  

  <LinearLayout

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:orientation="horizontal" >


    <Button

      android:id="@+id/SFA_BtnOK"

      android:layout_width="fill_parent"

      android:layout_height="wrap_content"

      android:layout_weight="1"

      android:text="OK" />


    <Button

      android:id="@+id/SFA_BtnCancel"

      android:layout_width="fill_parent"

      android:layout_height="wrap_content"

      android:layout_weight="1"

      android:text="Cancel" />

    

  </LinearLayout>

    

</LinearLayout>
</LinearLayout>

SaveFileActivity.java

package com.example.androidfiledialogs;

import java.io.File;
import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;


import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.os.Environment;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.Button;

import android.widget.EditText;

import android.widget.ListView;

import android.widget.Toast;

import android.widget.AdapterView.OnItemClickListener;


public class SaveFileActivity extends Activity

  implements OnClickListener, OnItemClickListener {


    ListView LvList;

    

    ArrayList<String> listItems = new ArrayList<String>();

    

    ArrayAdapter<String> adapter;

    

    EditText TxtFileName;

    

    Button BtnOK;

    Button BtnCancel;

    

    String currentPath = null;

    

  @Override

  protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_save_file);

    

    try {

      /* Initializing Widgets */

      LvList = (ListView) findViewById(R.id.SFA_LvList);

      TxtFileName = (EditText) findViewById(R.id.SFA_TxtFileName);

      BtnOK = (Button) findViewById(R.id.SFA_BtnOK);

      BtnCancel = (Button) findViewById(R.id.SFA_BtnCancel);

      

      /* Initializing Event Handlers */

      

      LvList.setOnItemClickListener(this);

      

      BtnOK.setOnClickListener(this);

      BtnCancel.setOnClickListener(this);

      

      //

      

      setCurrentPath(Environment.getExternalStorageDirectory().getAbsolutePath() + "/");

    } catch (Exception ex) {

      Toast.makeText(this,

          "Error in SaveFileActivity.onCreate: " + ex.getMessage(),

          Toast.LENGTH_SHORT).show();

    }

  }

  

  void setCurrentPath(String path) {

    ArrayList<String> folders = new ArrayList<String>();

    

    ArrayList<String> files = new ArrayList<String>();

    

    currentPath = path;

    

    File allEntries = new File(path).listFiles();

    

    for (int i = 0; i < allEntries.length; i++) {

      if (allEntries.isDirectory()) {

        folders.add(allEntries.getName());

      } else if (allEntries.isFile()) {

        files.add(allEntries.getName());

      }

    }

    

    Collections.sort(folders, new Comparator<String>() {

      @Override

      public int compare(String s1, String s2) {

        return s1.compareToIgnoreCase(s2);

      }

    });

    

    Collections.sort(files, new Comparator<String>() {

      @Override

      public int compare(String s1, String s2) {

        return s1.compareToIgnoreCase(s2);

      }

    });

    

    listItems.clear();

    

    for (int i = 0; i < folders.size(); i++) {

      listItems.add(folders.get(i) + "/");

    }

    

    for (int i = 0; i < files.size(); i++) {

      listItems.add(files.get(i));

    }

    

    adapter = new ArrayAdapter<String>(this,

        android.R.layout.simple_list_item_1,

        listItems);

    adapter.notifyDataSetChanged();

    

    LvList.setAdapter(adapter);

  }

  

  @Override

  public void onBackPressed()

  {

    if (!currentPath.equals(Environment.getExternalStorageDirectory().getAbsolutePath() + "/")) {

      setCurrentPath(new File(currentPath).getParent() + "/");

    } else {

      super.onBackPressed();

    }

  }

  

  @Override

  public void onClick(View v) {

    Intent intent;

    

    switch (v.getId()) {

    case R.id.SFA_BtnOK:

      

      intent = new Intent();

      intent.putExtra("fileName", currentPath + TxtFileName.getText().toString());

      intent.putExtra("shortFileName", TxtFileName.getText().toString());

      setResult(RESULT_OK, intent);

      

      this.finish();

      

      break;

    case R.id.SFA_BtnCancel:

      

      intent = new Intent();

      intent.putExtra("fileName", "");

      intent.putExtra("shortFileName", "");

      setResult(RESULT_CANCELED, intent);

      

      this.finish();

      

      break;

    }

  }


  @Override

  public void onItemClick(AdapterView<?> parent, View view, int position,

      long id) {

    String entryName = (String)parent.getItemAtPosition(position);

    if (entryName.endsWith("/")) {

      setCurrentPath(currentPath + entryName);

    } else {

      this.setTitle(this.getResources().getString(R.string.title_activity_open_file)

          + "<span>[</span>" + entryName + "]");

      

      TxtFileName.setText(entryName);

    }

  }

}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • android studio 清單配置文件androidmainfest.xml詳細解讀

    android studio 清單配置文件androidmainfest.xml詳細解讀

    AndroidManifest官方解釋是應用清單,每個應用的根目錄中都必須包含一個,并且文件名必須一模一樣,這個文件中包含了APP的配置信息,系統(tǒng)需要根據(jù)里面的內(nèi)容運行APP的代碼,顯示界面,這篇文章介紹了android studio 清單配置文件androidmainfest.xml解讀,需要的朋友可以參考下
    2024-04-04
  • 使用Chrome瀏覽器調(diào)試Android App詳解

    使用Chrome瀏覽器調(diào)試Android App詳解

    這篇文章主要介紹了使用Chrome瀏覽器調(diào)試Android App詳解,本網(wǎng)講解了使用Facebook開源Stetho實現(xiàn)在Chrome中調(diào)試Android App中,需要的朋友可以參考下
    2015-05-05
  • Android中TelephonyManager類的用法案例詳解

    Android中TelephonyManager類的用法案例詳解

    這篇文章主要介紹了Android中TelephonyManager類的用法,以獲取Android手機硬件信息為例詳細分析了TelephonyManager類的使用技巧,需要的朋友可以參考下
    2015-09-09
  • Android開發(fā)入門之Notification用法分析

    Android開發(fā)入門之Notification用法分析

    這篇文章主要介紹了Android中Notification用法,較為詳細的分析了Notification的功能、使用步驟與相關注意事項,需要的朋友可以參考下
    2016-07-07
  • Android之AttributeSet案例詳解

    Android之AttributeSet案例詳解

    這篇文章主要介紹了Android之AttributeSet案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • Android System fastboot 介紹和使用教程

    Android System fastboot 介紹和使用教程

    Fastboot是Android快速升級的一種方法,Fastboot的協(xié)議fastboot_protocol.txt在源碼目錄./bootable/bootloader/legacy下可以找到,本文給大家介紹Android System fastboot 介紹和使用教程,感興趣的朋友一起看看吧
    2024-01-01
  • Flutter實現(xiàn)切換應用時隱藏應用預覽

    Flutter實現(xiàn)切換應用時隱藏應用預覽

    如果您要顯示敏感數(shù)據(jù),例如錢包金額,或者只是當?shù)卿洷韱物@示插入的密碼清晰時,當您不在應用程序中時,您必須隱藏敏感數(shù)據(jù)。本文將利用Flutter實現(xiàn)切換應用時隱藏應用預覽,需要的可以參考一下
    2022-06-06
  • Android實現(xiàn)滑動選擇控件實例代碼

    Android實現(xiàn)滑動選擇控件實例代碼

    本篇文章主要介紹了Android實現(xiàn)滑動選擇控件實例代碼,非常具有實用價值,需要的朋友可以參考下。
    2017-03-03
  • Android實現(xiàn)圖片選擇器功能

    Android實現(xiàn)圖片選擇器功能

    這篇文章主要為大家詳細介紹了Android實現(xiàn)圖片選擇器功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • Android開發(fā)之圓角矩形創(chuàng)建工具RoundRect類定義與用法分析

    Android開發(fā)之圓角矩形創(chuàng)建工具RoundRect類定義與用法分析

    這篇文章主要介紹了Android開發(fā)之圓角矩形創(chuàng)建工具RoundRect類定義與用法,結(jié)合完整實例形式分析了Android圓角矩形工具類的定義與簡單使用技巧,需要的朋友可以參考下
    2018-01-01

最新評論

青浦区| 达孜县| 峡江县| 新巴尔虎右旗| 泰州市| 荆门市| 霍城县| 福建省| 内江市| 当雄县| 义马市| 马龙县| 景德镇市| 甘谷县| 平昌县| 北流市| 芒康县| 丹江口市| 清新县| 长泰县| 舞钢市| 彭阳县| 汪清县| 宁德市| 达州市| 藁城市| 霸州市| 新龙县| 老河口市| 子洲县| 阿拉善左旗| 中山市| 顺平县| 福州市| 古蔺县| 亳州市| 宁河县| 威宁| 石嘴山市| 中江县| 翁源县|