单击按钮后如何进行操作?

时间:2015-02-22 20:36:14

标签: java android button onclicklistener

我一直在论坛上寻找答案,但无论我修改它多少次,都会出现错误。它说“主要活动必须是抽象的,或者在onClickListener上实现抽象方法onClick(View)”这是我的代码。

import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.view.View;


public class MainActivity extends ActionBarActivity implements OnClickListener
{
  /*
   *Everything here is Array Strings and other variables that are unrelated to the problem.
   * I chose not to post them because they would have tooken to much unnecessary space
   */
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button button1, button2, button3;
    button1 = ((Button) findViewById(R.id.button1));
    button2 = ((Button) findViewById(R.id.button2));
    button3 = ((Button) findViewById(R.id.button3));
    button1.setOnClickListener(this);
    button2.setOnClickListener(this);
    button3.setOnClickListener(this);

         OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick( View v) {
            switch (v.getId()) {
                case R.id.button1:
                    //DO something
                    break;
                case R.id.button2:
                    //DO something
                    break;
                case R.id.button3:
                    //DO something
                    break;
            }
        }

};
}

4 个答案:

答案 0 :(得分:0)

如果您正在为此设置onClickListener,那么您不应该创建新的OnClickListener,onClick函数应该在活动类本身上。

答案 1 :(得分:0)

您不必在活动中创建新的OnClickListener,因为您的活动已经是OnclickListener,因为您已经实现了接口(但不完全使用这些方法)。只需实现Listener的方法。 它是两种可能性之一:在您的活动中实现接口或创建匿名侦听器。你已经做到了。

答案 2 :(得分:0)

"主要活动必须是抽象的,或者在onClickListener上实现抽象方法onClick(View)"意味着你的类缺少onClick()方法的实现。

阅读什么是界面?:http://docs.oracle.com/javase/tutorial/java/concepts/interface.html

使其有效的2种解决方案:

第一个解决方案: 你删除

implements OnClickListener

并将on set按钮定义为您在onCreate方法上定义的侦听器:

OnClickListener onClickListener = new OnClickListener() {
    @Override
    public void onClick( View v) {
        switch (v.getId()) {
            case R.id.button1:
                //DO something
                break;
            case R.id.button2:
                //DO something
                break;
            case R.id.button3:
                //DO something
                break;
        }
    }

button1.setOnClickListener(onClickListener);
button2.setOnClickListener(onClickListener);
button3.setOnClickListener(onClickListener);

第二个解决方案:

您在类本身中实现了onClick()方法:

public class MainActivity extends ActionBarActivity implements View.OnClickListener {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button button1, button2, button3;
    button1 = ((Button) findViewById(R.id.button1));
    button2 = ((Button) findViewById(R.id.button2));
    button3 = ((Button) findViewById(R.id.button3));
    button1.setOnClickListener(this);
    button2.setOnClickListener(this);
    button3.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.button1:
            //DO something
            break;
        case R.id.button2:
            //DO something
            break;
        case R.id.button3:
            //DO something
            break;
    }
}

}

答案 3 :(得分:-1)

当我通过他们的id搜索视图时,我找到了它们。但是,当我使用存储文本视图的变量时,它们无法识别,我不断被告知它们无处可寻。我的按钮不给我带来问题。只有textviews一直说“无法解析符号”

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button button1, button2, button3;
    button1 = ((Button) findViewById(R.id.button1));
    button2 = ((Button) findViewById(R.id.button2));
    button3 = ((Button) findViewById(R.id.button3));
    button1.setOnClickListener(this);
    button2.setOnClickListener(this);
    button3.setOnClickListener(this);
    TextView textview1, textview2, textview3, questionView, questionView2, questionNumber;

    textview1 = ((TextView) findViewById(R.id.textView1));
    textview2 = ((TextView) findViewById(R.id.textView2));
    textview3 = ((TextView) findViewById(R.id.textView3));
    questionView = ((TextView) findViewById(R.id.questionView));
    questionView2 = ((TextView) findViewById(R.id.questionView2));
    questionNumber = (TextView) findViewById(R.id.questionNumber);
}

   @Override

        public void onClick( View v) {
            switch (v.getId()) {
                case R.id.button1:



                    if(questionIndex < questions.length -1
                            )

                        if (questionIndex != 5){
                            textview2.setText("");
                        } if (questionIndex == 5){
                    textview2.setText(halfQues8);
                }

//确保索引在边界

                    questionIndex++ ; // increment the index


                    questionValue ++;



                    questionView.setText(Questions[questionIndex]); // set the text to the next question

    /*
     * set the text to the next answer options
     */
                    textview1.setText(answerButtA[questionIndex]);
                    textview2.setText(answerButtB[questionIndex]);
                    textview3.setText(answerButtC[questionIndex]);
                    questionNumber.setText("Question " + questionValueS + ".");

            }

            else{

                questionView.setText("Complete");
                questionNumber.setText("");
                questionView2.setText("");

                textview1.setText("");
                textview2.setText("");
                textview3.setText("");
                Results.setText("Click here for results");
            }






            break;
                case R.id.button2:
                    //DO something
                    break;
                case R.id.button3:
                    //DO something
                    break;
            }