我的程序正在编译没有问题,但现在无法解析OnClickListener ...我添加了额外的import语句并重新启动/重建,但没有快乐。不知道我是怎么打破它的!
package com.bignerdranch.android.gingerquiz;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class QuizActivity extends AppCompatActivity implements View.OnClickListener {
private Button mTrueButton;
private Button mFalseButton;
private Button mNextButton;
private TextView mQuestionTextView;
private Question[] mQuestionBank = new Question[] {
new Question(R.string.Question1, false),
new Question(R.string.Question2, false),
new Question(R.string.Question3, false),
new Question(R.string.Question4, false),
new Question(R.string.Question5, false),
};
private int mCurrentIndex = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
private void updateQuestion(){
int question = mQuestionBank[mCurrentIndex].getTextResId();
mQuestionTextView.setText(question);
}
mQuestionTextView = (TextView) findViewById(R.id.question_text_view);
mTrueButton=(Button) findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
checkAnswer(true);
}
});
mFalseButton=(Button) findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
checkAnswer(false);
}
});
mNextButton = (Button) findViewById(R.id.next_button);
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
mCurrentIndex = (mCurrentIndex +1) % mQuestionBank.length;
int question = mQuestionBank[mCurrentIndex].getTextResId();
mQuestionTextView.setText(question);
}
});
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
答案 0 :(得分:1)
正如斯巴达所说,你可以删除View.OnClickListener
,因为你正在为所有按钮使用单独的clickListener。
另一种方法:
您应该更改所有
mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
checkAnswer(true);
}
});
到
mTrueButton.setOnClickListener(this);
并放置一个onClick方法来处理与Ids相关的所有点击事件 像
@Override
public void onClick(View v) {
switch (view.getId())
{
//handle multiple view click events
}
}