如何在android中保存输入的状态?

时间:2015-06-09 14:11:18

标签: android

我遇到了保存输入状态的问题,直到现在我只保存edittext值。当savebtn点击所有状态应保存我想在我打开它时关闭我的应用程序后保存输入的值,它应该给我相同的输出?

主要Activity.Java

public class MainActivity extends Activity {
    CheckBox CheckBox1;
    private Button btn1;
    private Button btn2;
    private SharedPreferences savednotes;
    private EditText editText1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1= (Button)findViewById(R.id.Cnclbtn);
        btn2= (Button)findViewById(R.id.Savebtn);
        editText1 = (EditText) findViewById(R.id.editText1);
        savednotes = getSharedPreferences("notes",MODE_PRIVATE);
       editText1.setText(savednotes.getString("tag","Default"));
       CheckBox1 = (CheckBox) findViewById(R.id.checkBox1);


       //


        btn1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                finish();
                System.exit(0);
            }
        });
        btn2.setOnClickListener(saveButtonListener); 
    }
    private void makeTag(String tag){
        String or = savednotes.getString(tag, null);
         SharedPreferences.Editor preferencesEditor = savednotes.edit();
         preferencesEditor.putString("tag",tag); //change this line to this
         preferencesEditor.commit();
     }

    public OnClickListener saveButtonListener = new OnClickListener(){
    @Override
        public void onClick(View v) {

            makeTag(editText1.getText().toString());    

            ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(editText1.getWindowToken(),0);
        }};

    @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;
    }
} 

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/seekBar1"
        android:layout_centerHorizontal="true"
        android:text="Profiles"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="26dp"
        android:ems="10"
        android:hint="User Name" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/checkBox3"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="18dp"
        android:text="App Sound"
        android:layout_centerHorizontal="true"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="21dp" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/checkBox2"
        android:layout_centerVertical="true"
        android:text="Ring on cell" />

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/checkBox1"
        android:layout_centerHorizontal="true"
        android:text="Ring on msg" />

    <CheckBox
        android:id="@+id/checkBox3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/checkBox2"
        android:layout_centerHorizontal="true"
        android:text="Vibrate" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/checkBox3"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="31dp"
        android:entries="@array/dropdown"
        />

    <Button
        android:id="@+id/Savebtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toLeftOf="@+id/checkBox2"
        android:text="Save" />

    <Button
        android:id="@+id/Cnclbtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/Savebtn"
        android:layout_alignBottom="@+id/Savebtn"
        android:layout_toRightOf="@+id/checkBox2"
        android:text="Cancel" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:-1)

假设你有一个EditText,Spinner和一个CheckBox:

EditText txtName;
CheckBox chkAgree;
Spinner spnLevel;

protected void onCreate(Bundle savedInstanceState) {
    txtName = ...;
    chkAgree = ...;
    spnLevel = ...;

    btnSave.setOnClick(
    ...
    save();
    });

    load();
}

private void save(){
        SharedPreferences sharedPref = context.getSharedPreferences("my_app_prefs", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putString("name_value",txtName.getText().toString());
        editor.putInt("selected_level",spnLevel.getSelectedItemPosition());
        editor.putBoolean("is_agree",chkAgree.isChecked());
        editor.commit();
}//save()

private void load(){
         SharedPreferences sharedPref = context.getSharedPreferences("my_app_prefs", Context.MODE_PRIVATE);
         txtName.setText(sharedPref.getString("name_value",""));
         spnLevel.setSelection(sharedPref.getInt("selected_level",0));
         chkAgree.setChecked(sharedPref.getBoolean("is_agree",false));
}//save()

未编译,但应该是正确的。 根据您当前的课程

完成onCreate()