android-查看关键字是否存在的文件
来源:互联网 发布:js中on和bind的区别 编辑:程序博客网 时间:2024/05/21 10:23
package zhang.example;import java.io.File;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;public class search extends Activity {private Button ok;private TextView tv;private EditText et; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ok=(Button)findViewById(R.id.button1); tv=(TextView)findViewById(R.id.textView1); et=(EditText)findViewById(R.id.editText1); ok.setOnClickListener(new Button.OnClickListener(){@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubString key=et.getText().toString();if(key.equalsIgnoreCase("")){Toast.makeText(search.this, "xxx", Toast.LENGTH_SHORT).show();}else{tv.setText(searchFile(key));}}}); } private String searchFile(String key){ String result=""; File[] files=new File("/").listFiles(); for(File f:files){ if(f.getName().indexOf(key)>=0){ result+=f.getPath()+"\n"; } } if(result.equals("")) result="no";return result; }}
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="key word" /><EditText android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/editText1"></EditText><Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button><TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView></LinearLayout>