TextView / editText值更改

时间:2013-11-18 05:05:54

标签: android textview int android-edittext

我有一个按钮,当按下按钮时,会在单独的活动中更改TextView的值,显示EditText数量为1 --但是当它仅工作一次时,除非我放入,否则它将无法再次工作一个新的editText值然后它只能再次使用一次。

所以,如果我输入10,然后onClick .Text--,则会将其转到9,如果我onClick .Text-- 再次它仍将显示9。也许你可以看到我做错了什么?

 public void onClick(View v) {
             pref = getSharedPreferences("prefs", Context.MODE_PRIVATE);{

        CaAdd.amnt.setText(String.valueOf(CaAdd.amountt-1));
        newone = Integer.toString(CaAdd.amountt-1);
        Editor editor = pref.edit();
        res = "You have"  +" " + (String.valueOf(CaAdd.amountt-1))+" " + "Life left!";
        editor.putString("new",newone);
        editor.commit();
        Toast.makeText(getApplicationContext(), res ,Toast.LENGTH_SHORT).show();

持有TextView和EditText的活动CaAdd

    public void onClick(View v){


        Editor editor = pref.edit();

amountt++;
amountText = Integer.toString(amountt);
amountedit.setHint(amountText);
amnt.setText(amountText);



editor.putString("numbers", amountText);
editor.commit();





    }
});
ImageButton decrease = (ImageButton)mDateTimeDialogView.findViewById(R.id.decrease);
decrease.setOnClickListener(new OnClickListener(){

    public void onClick(View v){




        Editor editor = pref.edit();
      amountt--;
      amountText = Integer.toString(amountt);
      amountedit.setHint(amountText);
      amnt.setText(amountText);
      amount = amnt.getText().toString();
      editor.putString("numbers", amountText);
      editor.commit();

    }
         });
编辑:喜欢这个?

     pref = getSharedPreferences("prefs", Context.MODE_PRIVATE);{
  int   Am =    pref.getInt("numbers", Integer.valueOf(CaAdd.amountt));
CaAdd.amnt.setText(Am);
  Am--;

         pill = Integer.toString(Am);

        Editor editor = pref.edit();
        res = "You have"  +" " + (String.valueOf(CaAdd.amountt-1))+" " + "Life left!";
        editor.putString("new",pill);
        editor.commit();
        Toast.makeText(getApplicationContext(), res ,Toast.LENGTH_SHORT).show();

1 个答案:

答案 0 :(得分:1)

问题是您保存变量以增加减少值的方式。 在onclick之后,你从edittext获取值并存储但是你递减的值是amountt变量。你需要从偏好中获取值并减少它,显示它并保存到首选项