如果我按一个按钮然后按另一个按钮,则两个TextView上的数量相加。我希望它们是单独的数字。我试过谷歌找到解决方案,但我找不到任何东西。请帮帮忙?
public class MainActivity extends Activity {
int clicked = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
{
final TextView text = (TextView) findViewById(R.id.textView2);
final ImageButton button = (ImageButton)findViewById(R.id.imageButton2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
clicked++;
text.setText(" " + clicked + " ");
}
});
}
final TextView text = (TextView) findViewById(R.id.textView1);
text.setText("");
final ImageButton button = (ImageButton)findViewById(R.id.imageButton1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
clicked++;
text.setText(" " + clicked + " ");
}
});
}
}
答案 0 :(得分:0)
使用两个不同的变量。当您按任一按钮时,单击被添加到。同时为按钮选择不同的名称,不要将它们都命名为按钮。
例如:
public class MainActivity extends Activity {
int clicked1 = 0;
int clicked2 = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
{
final TextView text = (TextView) findViewById(R.id.textView2);
final ImageButton button2 = (ImageButton)findViewById(R.id.imageButton2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
clicked2++;
text.setText(" " + clicked2 + " ");
}
});
}
final TextView text = (TextView) findViewById(R.id.textView1);
text.setText("");
final ImageButton button1 = (ImageButton)findViewById(R.id.imageButton1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
clicked1++;
text.setText(" " + clicked1 + " ");
}
});
}
}