按Button更改EditText

typ*_*e34 1 android android-edittext

我有EditText并使用EditText中的数字作为我的变量.

我也有两个按钮,一个用于将EditText中的数字增加一个,另一个按钮减少一个按钮.

有人能告诉我代码使这成为可能吗?

Din*_*rma 5

使用EditText的xml中的以下行将输入类型设置为数字:

<EditText 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:inputType="number" 
            android:text="200"
            android:id="@+id/editText1" 
            android:layout_alignParentLeft="true">
        </EditText> 
Run Code Online (Sandbox Code Playgroud)

在源文件中使用以下代码将数字减少1:

final EditText ed=(EditText)findViewById(R.id.editText1);

        Button b1=(Button)findViewById(R.id.button01);

        b1.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

                int a=Integer.parseInt(ed.getText().toString());

                int b=a-1;

                ed.setText(new Integer(b).toString());
            }
        });
Run Code Online (Sandbox Code Playgroud)

同样,在xml中再添加一个按钮,将数字增加一个.