首页 > 代码库 > prefs

prefs

:界面(用的是之前建的一个界面 省了很多时间<TextView

        android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@android:color/transparent"
android:gravity="center"

android:textSize="22sp"/>
<EditText
android:id="@+id/ev_userName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="1234567890.+-*/%\n()"
android:hint="请输入用户名" />

<EditText
android:id="@+id/ev_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入年龄"/>


<CheckBox
android:id="@+id/chb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住用户名"/>
<LinearLayout
android:orientation="horizontal"
android:layout_height="0dp"
android:layout_width="wrap_content"
android:layout_weight="1">

<Button
android:id="@+id/btn_write"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="10dp"
android:text="写入"/>



<Button
android:id="@+id/btn_login"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读取"
android:textSize="20sp"/>
二:对其功能的实现
(第一步永远是定义相关组件)
public class MainActivity extends AppCompatActivity {
private EditText evuserName;
private EditText evpassword;
private Button btnLogin;
private CheckBox chkRemenber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

evuserName = (EditText) findViewById(R.id.ev_userName);
evpassword = (EditText) findViewById(R.id.ev_password);
btnLogin = (Button) findViewById(R.id.btn_login);
chkRemenber = (CheckBox) findViewById(R.id.chb);
String userName=readFromFile("data.txt");
if (!TextUtils.isEmpty(userName))
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String userName = evuserName.getText().toString();
String password = evpassword.getText().toString();
Toast.makeText(MainActivity.this, "用户名:" + userName +
"年龄:" + password, Toast.LENGTH_LONG).show();

if (chkRemenber.isChecked()) {
if (saveToFile(userName)) {
Toast.makeText(MainActivity.this,"保存成功",Toast.LENGTH_SHORT).show();
Intent intent=new Intent(MainActivity.this,MainActivity.class);


}
}

}
});
}
private String readFromFile(String filename) {
//1.打开输入流文件
//2.读取
try {
FileInputStream in=openFileInput(filename);
int length=in.available();
byte[]data=http://www.mamicode.com/new byte[length];
in.read(data);
return new String(data,"utf8");
} catch (java.io.IOException e) {
e.printStackTrace();
return null;
}

}

private boolean saveToFile(String userName) {
//1.打开文件
try {

FileOutputStream out=openFileOutput("data.txt",MODE_PRIVATE);
//2.写入
out.write(userName.getBytes());
//3.关闭文件输出流
out.close();
return true;
} catch (java.io.IOException e) {
e.printStackTrace();
return false;
}}
private boolean saveToPrefs(String userName){

SharedPreferences.Editor editor=getSharedPreferences("data.txt",MODE_PRIVATE).edit();
editor.putString("username",userName);
editor.putString("password",userName);
editor.putInt("age",18);
return true;
}
private void readFromPrefs(){
SharedPreferences preferences=getPreferences("data",MODE_PRIVATE);
String userName=preferences.getString("userName","");
if (!TextUtils.isEmpty(userName)){
evuserName.setText(userName);
}
int age=preferences.getInt("age",0);
evpassword.setText(age);
}

private SharedPreferences getPreferences(String data, int modePrivate) {
return null;
}
技术分享技术分享

prefs