Button不会根据TextView中的新值更新;只有先前设定的值

时间:2014-09-28 05:30:47

标签: java android

我有3个按钮,彼此互动。一个按钮减1,一个按钮增加1,第三个按钮将值重置为12(默认值)。

以下代码是我目前的代码。它不会在不离开活动的情况下发挥作用。目前,它将从值中减去1并添加1,但如果我按两次它,它将不会再次减去1。我试图做的是这样的事情:

default button sets to 12
press minus 1 = 11
press minus 1 = 10  
press minus 1 = 9
press minus 1 = 8
press plus 1 = 9

目前,它并没有这样的功能。它只是从Pref值中减去1,然后如果我再次点击它,则值不会改变。

public class Character1 extends ActionBarActivity {

SharedPreferences pref;
Random rndNumbers = new Random(); 
String twelve, getdisplay2; 
int d20    = rndNumbers.nextInt(20) +1; 
Button roll, minus1, plus1, newturn;
TextView display, display2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.character1);
    pref = getSharedPreferences("Character1", Context.MODE_PRIVATE);

    display2 = (TextView) findViewById(R.id.display2);
    minus1 = (Button) findViewById(R.id.minus1);
    plus1 = (Button) findViewById(R.id.plus1);
    newturn = (Button) findViewById(R.id.newturn);      

    roll = (Button) findViewById(R.id.d20);
    display = (TextView) findViewById(R.id.display1);
    getdisplay2 = pref.getString("display2", "");

    display2.setText(getdisplay2);

    TextWatcher watcher = new TextWatcher() {
        public void afterTextChanged(Editable s) {

            SharedPreferences.Editor editor=pref.edit();
            editor.putString("display2", display2.getText().toString());
            editor.commit();

        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            SharedPreferences.Editor editor=pref.edit();
            editor.putString("display2", display2.getText().toString());
            editor.commit();
        }
    };

    display2.addTextChangedListener(watcher);

    minus1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            float AP = NumberUtils.toFloat(getdisplay2);
            display2.setText(Float.toString(AP - 1));

            SharedPreferences.Editor editor=pref.edit();
            editor.putString("display2", display2.getText().toString());
            editor.commit();
        }
    });

    plus1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            float AP = NumberUtils.toFloat(getdisplay2);
            display2.setText(Float.toString(AP + 1));

            SharedPreferences.Editor editor=pref.edit();
            editor.putString("display2", display2.getText().toString());
            editor.commit();
        }
    });

    newturn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            display2.setText(Float.toString(12));

            SharedPreferences.Editor editor=pref.edit();
            editor.putString("display2", display2.getText().toString());
            editor.commit();
        }
    });

    roll.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                d20    = rndNumbers.nextInt(20) +1; 
                display.setText(" " + d20);

            }
        });

}

public void weavingstats (View view) 
{
    Intent intent = new Intent (this, WeavingPage.class);
    startActivity(intent);
}

public void combatstats (View view)
{
    Intent intent=new Intent (this, CombatPage.class);
    startActivity(intent);
}

public void charactersheetpage (View view)
{
    Intent intent=new Intent (this, CharacterCreationSheet1.class);
    startActivity(intent);
}

@Override
public void onBackPressed ()
{
    SharedPreferences.Editor editor=pref.edit();
    editor.putString("display2", display2.getText().toString());
    editor.commit();

    Intent intent = new Intent (this, MainActivity.class);
    startActivity(intent);
}

}

1 个答案:

答案 0 :(得分:0)

您必须在getdisplay2 = pref.getString("display2", "");的所有来电之前添加NumberUtils。类似的东西:

minus1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        getdisplay2 = pref.getString("display2", "");
        float AP = NumberUtils.toFloat(getdisplay2);
        display2.setText(Float.toString(AP - 1));

        SharedPreferences.Editor editor=pref.edit();
        editor.putString("display2", display2.getText().toString());
        editor.commit();
    }
});