0

我对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;
        }

    }
        }
    }
4

2 回答 2

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;
      }
     }
    }
  }   
于 2012-08-08T13:19:29.617 回答
0

再次查看您的代码后,我注意到您实际上正在设置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;
            }
        }
    }
}
于 2012-08-08T13:57:14.933 回答