作者在 2016-08-27 18:43:00 发布以下内容
public class MainActivity extends Activity {
String PASS = "myPassWord.txt";
Button addbtn, readbtn, modifybtn, savebtn;
EditText addet, modifyet;
TextView tv;
ScrollView sv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
addbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Write();
Toast.makeText(MainActivity.this, "新增成功!", 3000).show();
}
});
readbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this, "查询成功!", 3000).show();
Read();
}
});
}
public void Write() {// 增
String pass = addet.getText().toString();
try {
FileOutputStream fos = openFileOutput(PASS, MODE_APPEND);
try {
fos.write(pass.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
addet.setText("");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void Read() {// 查
FileInputStream fis;
InputStreamReader isr;
BufferedReader br;
try {
fis = openFileInput(PASS);
isr = new InputStreamReader(fis);
br = new BufferedReader(isr);
String s = "";
StringBuffer sb = new StringBuffer();
try {
while ((s = br.readLine()) != null) {
sb.append(s + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
tv.setText(sb);
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
isr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void findViews() {
addbtn = (Button) findViewById(R.id.button1);
readbtn = (Button) findViewById(R.id.button2);
addet = (EditText) findViewById(R.id.editText1);
sv = (ScrollView) findViewById(R.id.scrollView1);
tv = (TextView) findViewById(R.id.textView1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
///////////////////////////////////////////////////////////////
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请在此输入 亲友生日,重要记念日,等重大事件" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="新增" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查询全部" />
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp" />
</LinearLayout>
</ScrollView>
</LinearLayout>