SharedPreferences
package com.pb.android_107_7_01;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
// 声明所有的控件
public EditText edit_name, edit_password;
public Button button_save, button_search;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 实例化这几个控件
edit_name = (EditText) findViewById(R.id.edit_text_id);
edit_password = (EditText) findViewById(R.id.edit_text_id_1);
button_save = (Button) findViewById(R.id.button_save_id);
button_save.setOnClickListener(this);
button_search = (Button) findViewById(R.id.button_save_id_1);
button_search.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_save_id:
// 首先获得用户输入的内容
String name = edit_name.getText().toString().trim();
// 获得输入密码
String password = edit_password.getText().toString().trim();
// 写内容
try {
FileOutputStream fos = openFileOutput("data.txt",
Context.MODE_APPEND);
// 写用户名
fos.write(name.getBytes());
// 写密码进文件
fos.write(password.getBytes());
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 吐丝
Toast.makeText(MainActivity.this, "保存文件成功!!!", Toast.LENGTH_LONG)
.show();
break;
case R.id.button_save_id_1:
String str = "";
// 读写入文件的内容
try {
FileInputStream fis = openFileInput("data.txt");
// 缓存
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
str = new String(buffer);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(MainActivity.this, "查看保存内容" str, Toast.LENGTH_LONG)
.show();
break;
default:
break;
}
}
}
资源下载此资源下载价格为3D币,请先登录
资源文件列表
Android_107_7_01_Test/.classpath , 475
Android_107_7_01_Test/.project , 857
Android_107_7_01_Test/.settings/org.eclipse.core.resources.prefs , 57
Android_107_7_01_Test/.settings/org.eclipse.jdt.core.prefs , 177
Android_107_7_01_Test/AndroidManifest.xml , 849
Android_107_7_01_Test/bin/AndroidManifest.xml , 849
Android_107_7_01_Test/bin/classes/com/pb/android_107_7_01/BuildConfig.class , 355
Android_107_7_01_Test/bin/classes/com/pb/android_107_7_01/MainActivity.class , 2956
Android_107_7_01_Test/bin/classes/com/pb/android_107_7_01/R$attr.class , 355
Android_107_7_01_Test/bin/classes/com/pb/android_107_7_01/R$dimen.class , 476
Android_107_7_01_Test/bin/classes/com/pb/android_107_7_01/R$id.class , 684
Android_107_7_01_Test/bin/classes/com/pb/android_107_7_01/R$layout.class , 418
Android_107_7_01_Test/bin/classes/com/pb/android_107_7_01/R$menu.class , 403
Android_107_7_01_Test/bin/classes/com/pb/android_107_7_01/R$string.class , 487
Android_107_7_01_Test/bin/classes/com/pb/android_107_7_01/R$style.class , 446
Android_107_7_01_Test/bin/classes/com/pb/android_107_7_01/R.class , 664
Android_107_7_01_Test/gen/com/pb/android_107_7_01/BuildConfig.java , 165
Android_107_7_01_Test/gen/com/pb/android_107_7_01/R.java , 2822
Android_107_7_01_Test/proguard-project.txt , 781
Android_107_7_01_Test/project.properties , 563
Android_107_7_01_Test/res/layout/activity_main.xml , 2937
Android_107_7_01_Test/res/menu/main.xml , 263
Android_107_7_01_Test/res/values/dimens.xml , 220
Android_107_7_01_Test/res/values/strings.xml , 233
Android_107_7_01_Test/res/values/styles.xml , 697
Android_107_7_01_Test/res/values-sw600dp/dimens.xml , 203
Android_107_7_01_Test/res/values-sw720dp-land/dimens.xml , 277
Android_107_7_01_Test/res/values-v11/styles.xml , 334
Android_107_7_01_Test/res/values-v14/styles.xml , 391
Android_107_7_01_Test/src/com/pb/android_107_7_01/MainActivity.java , 2578