Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 搜索手機目錄遍歷

Android 搜索手機目錄遍歷

編輯:關於Android編程

package com.example.serach;


import java.io.File;


import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends
Activity {


Button mButton;
EditText mEditText;
TextView mTextView;


@Override
public void onCreate(
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button) findViewById(R.id.button1);
mEditText = (EditText) findViewById(R.id.editText1);
mTextView = (TextView) findViewById(R.id.textView1);


mButton.setOnClickListener(listener1);


}


OnClickListener listener1 = new OnClickListener() {


public void onClick(View v) {
// TODO Auto-generated method stub


Toast.makeText(
getApplicationContext(),
"loveyu", 2000)
.show();


String keywordString = mEditText
.getText()
.toString();


if (keywordString
.equals("")) {
mTextView
.setText("null");


}
else {
mTextView
.setText(searchFile(keywordString));
}


}


private CharSequence searchFile(
String keywordString) {
// TODO Auto-generated method stub


String resultString="";
File[] files=new File("/").listFiles();
for (File file : files) {
if (file.getName().indexOf(keywordString)>=0) {
resultString=file.getPath()+"\n"+resultString;
}
}

if (resultString.equals("")) {

resultString="找不到文件";
}
return resultString;
}
};


@Override
public boolean onCreateOptionsMenu(
Menu menu) {
getMenuInflater().inflate(
R.menu.activity_main,
menu);
return true;
}
}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved