第二章:java I/O應用

效果:


[img]http://dl.iteye.com/upload/attachment/388549/e8633f32-ec26-301d-a307-5310dfc73e11.jpg[/img]


main。xml


<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>

<EditText
android:id="@+id/text"
android:layout_width="194px"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_x="103px"
android:layout_y="23px"
>
</EditText>
<TextView
android:id="@+id/widget36"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="輸入搜索內容:"
android:layout_x="5px"
android:layout_y="34px"
>
</TextView>
<Button
android:id="@+id/search"
android:layout_width="157px"
android:layout_height="wrap_content"
android:text="搜素"
android:layout_x="118px"
android:layout_y="86px"
>
</Button>
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="16px"
android:layout_y="189px"
>
</TextView>

</AbsoluteLayout>





package i.o.test;

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;

public class IOTest extends Activity {
/** Called when the activity is first created. */
private Button bt;
private EditText text;
private TextView message;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt=(Button)findViewById(R.id.search);
text=(EditText)findViewById(R.id.text);
message=(TextView)findViewById(R.id.message);
bt.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
String str=text.getText().toString();
if(str.equals("")){
message.setText("輸入內容不能爲空!");
}else{
message.setText(searchFile(str));
}
}
});
}

public String searchFile(String str){
String result="";
File[] fils=new File("/").listFiles();
for(File f:fils){
if(f.getName().indexOf(str)>=0){
result=result+f.getPath()+"\n";
}
}
if(result.equals("")){
result="沒有該文件!";
}
return result;
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章