我是android和java的新手,到目前为止一直在做视频教程。然而,在我第一次尝试使用自己的代码时,Buttons
似乎没有回应我正在做的任何事情,这是错误的吗?
我已经完成了几个类似的代码并复制并粘贴了一些示例,并且在Android手机和平板电脑上运行的所有示例中都没有Buttons
做任何事情。
public class Two extends Activity implements OnClickListener {
Button showa;
EditText et1;
TextView ans1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.two);
methodInit();
showa.setOnClickListener(this);
}
private void methodInit() {
// TODO Auto-generated method stub
showa = (Button) findViewById(R.id.button1);
et1 = (EditText) findViewById(R.id.et1);
ans1 = (TextView) findViewById(R.id.ans1);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.button1:
String check = et1.getText().toString();
et1.setText(check);
if (check.contains("4")){
ans1.setText("correct!!!");
break;
}
}
}
}
答案 0 :(得分:0)
像这样使用
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.two);
methodInit();
}
private void methodInit() {
// TODO Auto-generated method stub
showa = (Button) findViewById(R.id.button1);
et1 = (EditText) findViewById(R.id.et1);
ans1 = (TextView) findViewById(R.id.ans1);
showa.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.button1:
String check = et1.getText().toString();
et1.setText(check);
if (check.contains("4")){
ans1.setText("correct!!!");
break;
}
}
}
}
答案 1 :(得分:0)
在第二次查看您的代码后,我注意到您 ARE 实际设置了onClickListener
...我在下面发布的代码对我有用。试一试,看看会发生什么。
您需要在onClickListener
上设置Button
。
看看以下代码:
public class Two extends Activity implements OnClickListener {
private Button showa;
private EditText et1;
private TextView ans1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.two);
methodInit();
}
private void methodInit() {
// TODO Auto-generated method stub
showa = (Button) findViewById(R.id.button1);
et1 = (EditText) findViewById(R.id.et1);
ans1 = (TextView) findViewById(R.id.ans1);
showa.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.button1:
String check = et1.getText().toString();
et1.setText(check);
if (check.contains("4")){
ans1.setText("correct!!!");
break;
}
}
}
}