我有EditText并使用EditText中的数字作为我的变量。按钮更改EditText
我也有两个按钮,一个用于增加EditText中的数字,另一个用于减少一个。
有人可以告诉我的代码,使这成为可能吗?
我有EditText并使用EditText中的数字作为我的变量。按钮更改EditText
我也有两个按钮,一个用于增加EditText中的数字,另一个用于减少一个。
有人可以告诉我的代码,使这成为可能吗?
使用中的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>
而在源文件中使用下面的代码被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());
}
});
同样,在xml中添加一个按钮以将数字增加1。
感谢家伙,我知道它不好,简单地问任何代码而没有给出任何自己的解决方案,但我真的不知道如何完成它。 – typie34 2011-06-16 17:25:43
你需要创建一个上点击收听每个按钮,做这样的事情:
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText editText = (EditText)findViewById(R.id.edit_text);
int newVal = ... //retrieve the previous val and increment it (or decrement it)
editText.setText(newVal, TextView.BufferType.EDITABLE);
}
});
button.setOnClickListener(new OnCLickListener(){
@Override
public void onClick(View arg0) {
if(arg0.equals(button1))
{
String s = editText.getText().toString();
Integer i = Integer.parseInt(s);
i=++i;
s = s.valueOf(i);
editText.setText(s);
}
if(arg0.equals(button2))
{
//decrement
}
}
})
只要你必须使用这两个按钮的点击事件,在增加键获取文本edittextbox并将其增加并将其设置在edittextbox中,对于递减按钮也是如此,并减小该值。
你不应该在这里要求代码.. :) – ngesh 2011-06-16 11:11:41