WEB开发网
开发学院手机开发Android 开发 Android 搜索手机目录遍历 阅读

Android 搜索手机目录遍历

 2012-09-14 15:04:25 来源:开发学院   
核心提示: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 and

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

Tags:Android 搜索 手机

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接