我正在尝试显示一个变量,用户应该能够使用2个按钮向上和向下计数。这是我的代码。问题是它似乎不起作用。如果你点击“minues”然后出现“1”,如果你点击“加”则没有任何反应。非常感谢!
我知道切换可能不是最优雅的解决方案,但我还是想尝试一下
public class StoryCreator extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_story_creator);
Button b1 = (Button) findViewById(R.id.minus);
b1.setOnClickListener(this);
Button b2 = (Button) findViewById(R.id.plus);
b2.setOnClickListener(this);
}
public void onClick(View v) {
int Zahl = 1;
TextView objekt = (TextView) findViewById(R.id.textView1);
objekt.setText("0");
// Perform action on click
switch(v.getId())
{
case 1:
Zahl = Zahl-1;
break;
case 2:
Zahl = Zahl+1;
break;
default:
break;
}
switch(Zahl) {
case -1:
objekt.setText("-1");
break;
case 0:
objekt.setText("0");
break;
case 1:
objekt.setText("1");
break;
case 2:
objekt.setText("2");
break;
case 3:
objekt.setText("3");
break;
}
答案 0 :(得分:2)
将您的开关案例更改为:
// Perform action on click
switch(v.getId())
{
case R.id.minus:
Zahl = Zahl-1;
break;
case R.id.plus:
Zahl = Zahl+1;
break;
default:
break;
}
使用Button id代替1,2来执行按钮单击
的操作答案 1 :(得分:0)
你也设置了
int Zahl = 1;
每按一次按钮,Zahl的值可能只有0或1
public class StoryCreator extends Activity implements OnClickListener {
int mValue = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_story_creator);
Button b1 = (Button) findViewById(R.id.minus);
b1.setOnClickListener(this);
Button b2 = (Button) findViewById(R.id.plus);
b2.setOnClickListener(this);
}
public void onClick(View v) {
TextView objekt = (TextView) findViewById(R.id.textView1);
// Perform action on click
switch(v.getId())
{
case R.id.plus:
mValue++;
break;
case R.id.minus:
mValue--;
break;
default:
break;
}
objekt.setText(mValue);
}
答案 2 :(得分:0)
嗯,您的代码中存在许多问题:
尝试使用此代码:
public class StoryCreator extends Activity implements OnClickListener {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_story_creator);
Button b1 = (Button) findViewById(R.id.minus);
b1.setOnClickListener(this);
Button b2 = (Button) findViewById(R.id.plus);
b2.setOnClickListener(this);
textView = (TextView) findViewById(R.id.textView1);
textView.setText("0"); //initial value
}
public void onClick(View v) {
int Zahl = Integer.parseInt(textView.getText().toString());
// Perform action on click
switch(v.getId())
{
case R.id.plus:
Zahl++;
break;
case R.id.minus:
Zahl--;
break;
default:
break;
}
textView.setText(String.valueOf(Zahl));
}
}
答案 3 :(得分:0)
更简洁的方法是删除“implements View.OnClickListener”并为每个按钮创建单击侦听器。
此外,上述解决方案仍然没有实际增加单个变量。如果您希望增量在实例之间保持不变,则需要将其变为静态。
以下内容可为您提供所需内容,并且无需使用View.OnClickListener污染您的活动即可干净利落。
public class StoryCreator extends Activity {
TextView textView;
private static int zahl = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_story_creator);
Button minus = (Button) findViewById(R.id.minus);
minus.setOnClickListener(minusClickListener);
Button plus = (Button) findViewById(R.id.plus);
plus.setOnClickListener(plusClickListener);
textView = (TextView) findViewById(R.id.textView1);
textView.setText("0"); //initial value
}
private View.OnClickListener minusClickListener= new View.OnClickListener() {
public void onClick(View v) {
zahl--;
}
}
private View.OnClickListener plusClickListener = new View.OnClickListener() {
public void onClick(View v) {
zahl++;
}
}
答案 4 :(得分:0)
对不起它有点乱,但这应该完全符合你的要求。
public class CountUpAction extends AbstractAction {
/** Constructor */
public CountUpAction(String name, String shortDesc, Integer mnemonic) {
super(name);
putValue(SHORT_DESCRIPTION, shortDesc);
putValue(MNEMONIC_KEY, KeyEvent.VK_NUMPAD4);
}
@Override
public void actionPerformed(ActionEvent e) {
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent evt) {
switch(evt.getKeyCode()) {
case KeyEvent.VK_NUMPAD1:
//original count
count += 1;
tfCount.setText(count + "");
}
}
});
}
}
public class CountDownAction extends AbstractAction {
/** Constructor */
public CountDownAction(String name, String shortDesc, Integer mnemonic) {
super(name);
putValue(SHORT_DESCRIPTION, shortDesc);
putValue(MNEMONIC_KEY, KeyEvent.VK_NUMPAD5);
}
@Override
public void actionPerformed(ActionEvent e) {
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent evt) {
switch(evt.getKeyCode()) {
case KeyEvent.VK_NUMPAD2:
//original count
count -= 1;
tfCount.setText(count + "");
}
}
});
}
Action countUpAction = new CountUpAction("Count Up",
"", new Integer(KeyEvent.VK_ENTER));
Action countDownAction = new CountDownAction("Count Down",
"", new Integer(KeyEvent.VK_D));
tfCount = new JTextField("0", 8);
tfCount.setHorizontalAlignment(JTextField.CENTER);
tfCount.setEditable(false);
tfCount.setSize(100, 20);
canvas.add(tfCount, BorderLayout.LINE_START);
//Invisible
JButton btnCountUp = new JButton();
btnCountUp.setFocusable(false);
btnCountUp.setHideActionText(true);
btnCountUp.setContentAreaFilled(false);
btnCountUp.setBorderPainted(false);
canvas.add(btnCountUp, BorderLayout.NORTH);
//Invisible
JButton btnCountDown = new JButton();
btnCountDown.setFocusable(false);
btnCountDown.setHideActionText(true);
btnCountDown.setContentAreaFilled(false);
btnCountDown.setBorderPainted(false);
canvas.add(btnCountDown);
// Set actions for buttons
btnCountUp.setAction(countUpAction);
btnCountDown.setAction(countDownAction);