欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

非常簡(jiǎn)單的Android打開和保存對(duì)話框功能

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

在Android上沒有標(biāo)準(zhǔn)的打開和另存為對(duì)話框。在本代碼中,我將詳細(xì)描述一個(gè)非常簡(jiǎn)單的打開和保存對(duì)話框?qū)崿F(xiàn)過程,對(duì)于Android初學(xué)者來說非常有用,對(duì)話框都是全屏活動(dòng)的。

主要功能:

1、訪問任何目錄的SD卡
2、遞歸訪問文件夾
3、單一文件選擇
4、通過按硬件后退按鈕升級(jí)
5、確認(rèn)文件選擇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);

    }

  }

}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 淺談Android Studio 4.1 更新內(nèi)容

    淺談Android Studio 4.1 更新內(nèi)容

    這篇文章主要介紹了淺談Android Studio 4.1 更新內(nèi)容,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • Android自定義簡(jiǎn)單的頂部標(biāo)題欄

    Android自定義簡(jiǎn)單的頂部標(biāo)題欄

    這篇文章主要為大家詳細(xì)介紹了Android自定義簡(jiǎn)單的頂部標(biāo)題欄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • Android 實(shí)現(xiàn)獲取手機(jī)里面的所有圖片詳解及實(shí)例

    Android 實(shí)現(xiàn)獲取手機(jī)里面的所有圖片詳解及實(shí)例

    這篇文章主要介紹了Android 實(shí)現(xiàn)獲取手機(jī)里面的所有圖片詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • 自定義搜索功能Android實(shí)現(xiàn)

    自定義搜索功能Android實(shí)現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了自定義搜索功能,由Android代碼實(shí)現(xiàn),感興趣的小伙伴們可以參考一下
    2016-05-05
  • Android Intent實(shí)現(xiàn)頁面跳轉(zhuǎn)的方法示例

    Android Intent實(shí)現(xiàn)頁面跳轉(zhuǎn)的方法示例

    本篇文章主要介紹了Android Intent實(shí)現(xiàn)頁面跳轉(zhuǎn)的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-03-03
  • 深入學(xué)習(xí)Android?ANR?的原理分析及解決辦法

    深入學(xué)習(xí)Android?ANR?的原理分析及解決辦法

    Android系統(tǒng)中,AMS和WMS會(huì)檢測(cè)App的響應(yīng)時(shí)間,如果App在特定時(shí)間無法相應(yīng)屏幕觸摸或鍵盤輸入時(shí)間,或者特定事件沒有處理完畢,就會(huì)出現(xiàn)ANR。本文將帶領(lǐng)大學(xué)深入學(xué)習(xí)一下ANR的原理及解決辦法,感興趣的同學(xué)可以學(xué)習(xí)一下
    2021-11-11
  • Android開發(fā) -- setTag的妙用和The key must be an application-specific resource id 異常

    Android開發(fā) -- setTag的妙用和The key must be an application-specif

    本文主要介紹Android開發(fā)setTag的妙用,小編覺得挺實(shí)用的,給大家一個(gè)參考,希望對(duì)大家學(xué)習(xí)有所幫助。
    2016-06-06
  • Android設(shè)置PreferenceCategory背景顏色的方法

    Android設(shè)置PreferenceCategory背景顏色的方法

    這篇文章主要介紹了Android設(shè)置PreferenceCategory背景顏色的方法,涉及Android設(shè)置背景色的技巧,需要的朋友可以參考下
    2015-05-05
  • android studio按鈕監(jiān)聽的5種方法實(shí)例詳解

    android studio按鈕監(jiān)聽的5種方法實(shí)例詳解

    這篇文章主要介紹了android studio按鈕監(jiān)聽的5種方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • Android對(duì)話框AlertDialog詳解

    Android對(duì)話框AlertDialog詳解

    本文詳細(xì)講解了Android對(duì)話框AlertDialog的實(shí)現(xiàn)方式,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-12-12

最新評(píng)論