手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >非常简单的Android打开和保存对话框功能
非常简单的Android打开和保存对话框功能
摘要:在Android上没有标准的打开和另存为对话框。在本代码中,我将详细描述一个非常简单的打开和保存对话框实现过程,对于Android初学者来说...

在Android上没有标准的打开和另存为对话框。在本代码中,我将详细描述一个非常简单的打开和保存对话框实现过程,对于Android初学者来说非常有用,对话框都是全屏活动的。

主要功能:

1、访问任何目录的SD卡

2、递归访问文件夹

3、单一文件选择

4、通过按硬件后退按钮升级

5、确认文件选择OK按钮

activity_open_file.xml

<LinearLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android"" rel="nofollow" target="_blank">http://schemas.android.com/apk/res/android"</a> xmlns:tools="<a href="http://schemas.android.com/tools"" 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 href="http://schemas.android.com/apk/res/android"" rel="nofollow" target="_blank">http://schemas.android.com/apk/res/android"</a> xmlns:tools="<a href="http://schemas.android.com/tools"" 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); } } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持查字典教程网。

【非常简单的Android打开和保存对话框功能】相关文章:

Android单选对话框和多选对话框

Android 各国语言缩写及简称详细介绍

苹果MAC中安装并搭建Android开发环境的详细步骤

Android UI开发 View自绘控件 分享

Android UI开发专题(一) 之界面设计

Android 开机广播的使用及配置

Android开发中如何获取应用版本号

安装android开发环境原始版(windows版)

Android开发常用小功能

Android开发之相对布局

精品推荐
分类导航