SharePreferenceDemo

来源:互联网 发布:linux dhcp服务安装包 编辑:程序博客网 时间:2024/05/17 04:55

MainActivity.java


public class MainActivity extends Activity {

    EditText edt1, edt2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        

        Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                edt1 = (EditText) findViewById(R.id.edt1);
                edt2 = (EditText) findViewById(R.id.edt2);
                String user = edt1.getText().toString();
                String password = edt2.getText().toString();
                SharedPreferences sharedPreferences = getSharedPreferences(
                        "setting", Context.MODE_PRIVATE); // 私有数据
                Editor editor = sharedPreferences.edit();// 获取编辑器
                editor.putString("username", user);
                editor.putString("userpassword", password);
                editor.commit();// 提交修改
            }
        });

        // Button button2 = (Button) findViewById(R.id.button2);
        // button2.setOnClickListener(new OnClickListener() {
        //
        // @Override
        // public void onClick(View v) {
        // TODO Auto-generated method stub
        
        /**
         * 自动加载sharepreference文件,并填写
         */
        SharedPreferences share = getSharedPreferences("setting",
                Context.MODE_PRIVATE);
        String username = share.getString("username", "");
        String userpassword = share.getString("userpassword", "");
        
        edt1 = (EditText) findViewById(R.id.edt1);
        edt2 = (EditText) findViewById(R.id.edt2);
        
        edt1.setText(username);
        edt2.setText(userpassword);
//
//        TextView text1 = (TextView) findViewById(R.id.text1);
//        TextView text2 = (TextView) findViewById(R.id.text2);
//
//        text1.setText(username);
//        text2.setText(userpassword);
        // }
        // });

    }

}


activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/edt1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="输入账号" />

    <EditText
        android:id="@+id/edt2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="输入密码" />

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="保存数据" />

    <Button
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="显示数据" />

    <TextView
        android:id="@+id/text1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="null" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="null" />

</LinearLayout>

0 0
原创粉丝点击