package com.inc.increment;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Increment extends Activity implements OnClickListener {
int a,b;
Button b1,b2;
EditText editText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_increment);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(this);
b2 = (Button) findViewById(R.id.button2);
b2.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_increment, menu);
return true;
}
public void onClick(View v) {
// now perform the increment/decrement
if (R.id.button1 == v.getId()) {
editText = (EditText) findViewById(R.id.timepicker_input);
a=Integer.parseInt(editText.getText().toString());
b=a+1;
String s1 = String.valueOf(b);
editText.setText(s1);
} else if (R.id.button2 == v.getId()) {
editText = (EditText) findViewById(R.id.timepicker_input);
int a=Integer.parseInt(editText.getText().toString());
int b=a-1;
String s1 = String.valueOf(b);
editText.setText(s1);
}
}
}
问问题
2613 次
3 回答
1
您是否为 timepicker_input 设置了默认值?如果它在活动 onCreate 期间为空白,并且 onclick 将出错,因为 parseInt 将因 Numberformatexception 而失败...
你的错误是什么?
于 2012-07-07T06:22:18.263 回答
1
请为此使用以下代码。
public class MainActivity extends Activity implements OnClickListener {
Button b1, b2;
EditText editText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText = (EditText) findViewById(R.id.timepicker_input);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(this);
b2 = (Button) findViewById(R.id.button2);
b2.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options_menu, menu);
return true;
}
public void onClick(View v) {
// now perform the increment/decrement
if (R.id.button1 == v.getId()) {
if (editText.getText().toString().equals(null)
|| editText.getText().toString().equals("")
|| editText.getText().toString().equals("null")) {
Toast.makeText(this, "Enter Some Value", Toast.LENGTH_SHORT)
.show();
} else {
int a = Integer.parseInt(editText.getText().toString());
int b = a + 1;
String s1 = String.valueOf(b);
editText.setText(s1);
}
} else if (R.id.button2 == v.getId()) {
if (editText.getText().toString().equals(null)
|| editText.getText().toString().equals("")
|| editText.getText().toString().equals("null")) {
Toast.makeText(this, "Enter Some Value", Toast.LENGTH_SHORT)
.show();
} else {
int a = Integer.parseInt(editText.getText().toString());
int b = a - 1;
String s1 = String.valueOf(b);
editText.setText(s1);
}
}
}
}
于 2012-07-07T07:30:52.643 回答
0
我对您的代码进行了一些更改,使用下面的代码,它将起作用。
package com.inc.increment;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Increment extends Activity implements OnClickListener {
int a,b;
Button b1,b2;
EditText editText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_increment);
editText = (EditText) findViewById(R.id.timepicker_input);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(this);
b2 = (Button) findViewById(R.id.button2);
b2.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_increment, menu);
return true;
}
public void onClick(View v) {
// now perform the increment/decrement
if (R.id.button1 == v.getId()) {
a=Integer.parseInt(editText.getText().toString());
b=a+1;
String s1 = String.valueOf(b);
editText.setText(s1);
} else if (R.id.button2 == v.getId()) {
a=Integer.parseInt(editText.getText().toString());
b=a-1;
String s1 = String.valueOf(b);
editText.setText(s1);
}
}
}
不要在点击时创建edittext实例,它应该在oncreate中
于 2012-07-07T06:24:30.287 回答