有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用共享首选项在单击按钮时保存数据

我正在进行一项活动,我的目标是通过单击“保存”按钮向我显示数据、输入的值,然后单击“编辑”按钮允许我编辑数据、保存的值。我有一些文本视图和编辑文本以及两个按钮

´´´
public class MyInfo extends AppCompatActivity {
    SharedPreferences sharedpreferences;
    TextView name;
    TextView address;
    TextView firstContactName;
    TextView firstContactPhoneNumber;
    TextView secondContactName;
    TextView secondContactPhoneNumber;
    public static final String mypreference = "mypref";
    public static final String Name = "nameKey";
    public static final String Address = "addressKey";
    public static final String FirstContactName = "firstContactNameKey";
    public static final String SecondContactName = "secondContactNameKey";
    public static final String FirstContactPhoneNumber = "firstContactPhoneNumberKey";
    public static final String SecondContactPhoneNumber = "secondContactPhoneNumber";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_info);
        name = (TextView) findViewById(R.id.edit_text_my_name);
        address = (TextView) findViewById(R.id.edit_text_my_address);
        firstContactName = (TextView) findViewById(R.id.edit_text_first_contact_name);
        firstContactPhoneNumber = (TextView) findViewById(R.id.edit_text_first_contact_phone);
        secondContactName = (TextView) findViewById(R.id.edit_text_second_contact_name);
        secondContactPhoneNumber = (TextView) findViewById(R.id.edit_text_second_contact_phone);
        sharedpreferences = getSharedPreferences(mypreference,
                Context.MODE_PRIVATE);
        if (sharedpreferences.contains(Name)) {
            name.setText(sharedpreferences.getString(Name, ""));
        }
        if (sharedpreferences.contains(Address)) {
            address.setText(sharedpreferences.getString(Address, ""));
        }
        if (sharedpreferences.contains(FirstContactName)) {
            firstContactName.setText(sharedpreferences.getString(FirstContactName, ""));
        }
        if (sharedpreferences.contains(FirstContactPhoneNumber)) {
            firstContactPhoneNumber.setText(sharedpreferences.getString(FirstContactPhoneNumber, ""));
        }
        if (sharedpreferences.contains(SecondContactName)) {
            secondContactName.setText(sharedpreferences.getString(SecondContactName, ""));
        }
        if (sharedpreferences.contains(SecondContactPhoneNumber)) {
            secondContactPhoneNumber.setText(sharedpreferences.getString(SecondContactPhoneNumber, ""));
        }
    }

    public void Save(View view) {

        String n = name.getText().toString();
        String a = address.getText().toString();
        String fcn = firstContactName.getText().toString();
        String fcpn = firstContactPhoneNumber.getText().toString();
        String scn = secondContactName.getText().toString();
        String scpn = secondContactPhoneNumber.getText().toString();
        SharedPreferences.Editor editor = sharedpreferences.edit();
        editor.putString(Name, n);
        editor.putString(Address, a);
        editor.putString(FirstContactName, fcn);
        editor.putString(FirstContactPhoneNumber, fcpn);
        editor.putString(SecondContactName, scn);
        editor.putString(SecondContactPhoneNumber, scpn);
        editor.apply();

        name = (TextView) findViewById(R.id.edit_text_my_name);
        address = (TextView) findViewById(R.id.edit_text_my_address);
        firstContactName = (TextView) findViewById(R.id.edit_text_first_contact_name);
        firstContactPhoneNumber = (TextView) findViewById(R.id.edit_text_first_contact_phone);
        secondContactName = (TextView) findViewById(R.id.edit_text_second_contact_name);
        secondContactPhoneNumber = (TextView) findViewById(R.id.edit_text_second_contact_phone);
        sharedpreferences = getSharedPreferences(mypreference,
                Context.MODE_PRIVATE);

        if (sharedpreferences.contains(Name)) {
            name.setText(sharedpreferences.getString(Name, ""));
        }
        if (sharedpreferences.contains(Address)) {
            address.setText(sharedpreferences.getString(Address, ""));
        }
        if (sharedpreferences.contains(FirstContactName)) {
            firstContactName.setText(sharedpreferences.getString(FirstContactName, ""));
        }
        if (sharedpreferences.contains(FirstContactPhoneNumber)) {
            firstContactPhoneNumber.setText(sharedpreferences.getString(FirstContactPhoneNumber, ""));
        }
        if (sharedpreferences.contains(SecondContactName)) {
            secondContactName.setText(sharedpreferences.getString(SecondContactName, ""));
        }
        if (sharedpreferences.contains(SecondContactPhoneNumber)) {
            secondContactPhoneNumber.setText(sharedpreferences.getString(SecondContactPhoneNumber, ""));
        }
    }
}
´´´

我已经考虑过了,但是我不能让它工作,我不知道我的java代码出了什么问题

'

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:app="http://schemas.安卓.com/apk/res-auto"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:paddingLeft="@dimen/padding_left_right"
    安卓:paddingRight="@dimen/padding_left_right"
    安卓:paddingTop="@dimen/padding_top_bottom"
    安卓:paddingBottom="@dimen/padding_top_bottom"
    安卓:orientation="vertical"
    tools:context=".MyInfo">


    <TextView
        安卓:id="@+id/text_view_my_name"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:text="@string/my_name"
        安卓:textAlignment="center"
        安卓:layout_gravity="center_horizontal"/>

    <EditText
        安卓:id="@+id/edit_text_my_name"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content" />

    <TextView
        安卓:id="@+id/text_view_my_address"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:text="@string/my_address"
        安卓:textAlignment="center"
        安卓:layout_gravity="center_horizontal" />

    <EditText
        安卓:id="@+id/edit_text_my_address"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content" />

    <TextView
        安卓:id="@+id/text_view_first_contact_name"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:text="@string/first_contact_name"
        安卓:textAlignment="center"
        安卓:layout_gravity="center_horizontal" />

    <EditText
        安卓:id="@+id/edit_text_first_contact_name"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content" />

    <TextView
        安卓:id="@+id/text_view_first_contact_phone"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:text="@string/first_contact_phone"
        安卓:textAlignment="center"
        安卓:layout_gravity="center_horizontal" />

    <EditText
        安卓:id="@+id/edit_text_first_contact_phone"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content" />

    <TextView
        安卓:id="@+id/text_view_second_contact_name"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:text="@string/second_contact_name"
        安卓:textAlignment="center"
        安卓:layout_gravity="center_horizontal" />

    <EditText
        安卓:id="@+id/edit_text_second_contact_name"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content" />

    <TextView
        安卓:id="@+id/text_view_second_contact_phone"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:text="@string/second_contact_phone"
        安卓:textAlignment="center"
        安卓:layout_gravity="center_horizontal" />

    <EditText
        安卓:id="@+id/edit_text_second_contact_phone"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content" />
    <LinearLayout
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:orientation="horizontal"
        安卓:layout_gravity="center">

        <Button
            安卓:id="@+id/edit_button"
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:onClick="Clear"
            安卓:text="@string/edit_button" />

        <Button
            安卓:id="@+id/save_button"
            安卓:layout_width="match_parent"
            安卓:layout_height="wrap_content"
            安卓:onClick="Save"
            安卓:text="@string/save_button" />

    </LinearLayout>

</LinearLayout>

'


共 (2) 个答案

  1. # 1 楼答案

    你所有的文本字符串似乎都来自“文本视图”。我看不到要编辑值的“编辑文本”。将要编辑的值更改为显示在“EditText”而不是“TextView”中

  2. # 2 楼答案

    首先初始化小部件,然后保存共享优先权上的值

     if (sharedpreferences.contains(Name)) {
            name.setText(sharedpreferences.getString(Name, ""));
        }
        if (sharedpreferences.contains(Address)) {
            address.setText(sharedpreferences.getString(Address, ""));
        }
        if (sharedpreferences.contains(FirstContactName)) {
            firstContactName.setText(sharedpreferences.getString(FirstContactName, ""));
        }
        if (sharedpreferences.contains(FirstContactPhoneNumber)) {
            firstContactPhoneNumber.setText(sharedpreferences.getString(FirstContactPhoneNumber, ""));
        }
        if (sharedpreferences.contains(SecondContactName)) {
            secondContactName.setText(sharedpreferences.getString(SecondContactName, ""));
        }
        if (sharedpreferences.contains(SecondContactPhoneNumber)) {
            secondContactPhoneNumber.setText(sharedpreferences.getString(SecondContactPhoneNumber, ""));
        }
    
     name = (TextView) findViewById(R.id.edit_text_my_name);
        address = (TextView) findViewById(R.id.edit_text_my_address);
        firstContactName = (TextView) findViewById(R.id.edit_text_first_contact_name);
        firstContactPhoneNumber = (TextView) findViewById(R.id.edit_text_first_contact_phone);
        secondContactName = (TextView) findViewById(R.id.edit_text_second_contact_name);
        secondContactPhoneNumber = (TextView) findViewById(R.id.edit_text_second_contact_phone);
        sharedpreferences = getSharedPreferences(mypreference,
                Context.MODE_PRIVATE);
    
    
      String n = name.getText().toString();
        String a = address.getText().toString();
        String fcn = firstContactName.getText().toString();
        String fcpn = firstContactPhoneNumber.getText().toString();
        String scn = secondContactName.getText().toString();
        String scpn = secondContactPhoneNumber.getText().toString();
        SharedPreferences.Editor editor = sharedpreferences.edit();
        editor.putString(Name, n);
        editor.putString(Address, a);
        editor.putString(FirstContactName, fcn);
        editor.putString(FirstContactPhoneNumber, fcpn);
        editor.putString(SecondContactName, scn);
        editor.putString(SecondContactPhoneNumber, scpn);
        editor.apply();