非常簡單的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 常見的四種對話框?qū)嵗v解
- Android 對話框(Dialog)大全詳解及示例代碼
- Android 對話框 Dialog使用實例講解
- Android對話框自定義標題 對話框標題美化操作
- 懸浮對話框Android代碼實現(xiàn)
- Android Dialog 對話框詳解及示例代碼
- Android Dialog對話框用法實例詳解
- Android UI設(shè)計系列之自定義Dialog實現(xiàn)各種風格的對話框效果(7)
- Android中制作自定義dialog對話框的實例分享
- Android中AlertDialog各種對話框的用法實例詳解
- Android 對話框sweet-alert-dialog
相關(guān)文章
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詳解,本網(wǎng)講解了使用Facebook開源Stetho實現(xiàn)在Chrome中調(diào)試Android App中,需要的朋友可以參考下2015-05-05
Android中TelephonyManager類的用法案例詳解
這篇文章主要介紹了Android中TelephonyManager類的用法,以獲取Android手機硬件信息為例詳細分析了TelephonyManager類的使用技巧,需要的朋友可以參考下2015-09-09
Android開發(fā)入門之Notification用法分析
這篇文章主要介紹了Android中Notification用法,較為詳細的分析了Notification的功能、使用步驟與相關(guān)注意事項,需要的朋友可以參考下2016-07-07
Android System fastboot 介紹和使用教程
Fastboot是Android快速升級的一種方法,Fastboot的協(xié)議fastboot_protocol.txt在源碼目錄./bootable/bootloader/legacy下可以找到,本文給大家介紹Android System fastboot 介紹和使用教程,感興趣的朋友一起看看吧2024-01-01
Android開發(fā)之圓角矩形創(chuàng)建工具RoundRect類定義與用法分析
這篇文章主要介紹了Android開發(fā)之圓角矩形創(chuàng)建工具RoundRect類定義與用法,結(jié)合完整實例形式分析了Android圓角矩形工具類的定義與簡單使用技巧,需要的朋友可以參考下2018-01-01

