当前位置:操作系统 > 安卓/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;
}
}

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,