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;
}
}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。