如何解决有关setOnClickListener的问题

时间:2019-03-23 04:45:20

标签: java android

我无法解决setOnClickListener

public class MainActivity extends AppCompatActivity {

    private Button btn1, btn2, btn3, btnC;
    private EditText edit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        edit = (EditText)findViewById(R.id.editText);
        btn1 = (Button)findViewById(R.id.btnOne);
        btn2 = (Button)findViewById(R.id.btnTwo);
        btn3 = (Button)findViewById(R.id.btnThree);
        btnC = (Button)findViewById(R.id.btnClear);


        OnClickListener cl = new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v == btn1)
                    edit.setText(edit.getText().toString() + 1);
                else if (v == btn2)
                    edit.setText(edit.getText().toString() + 2);
                else if (v == btn3)
                    edit.setText(edit.getText().toString() + 3);
                else if (v == btnC)
                    edit.setText("");
            }


// error line

        btn1.setOnClickListener(cl);

        btn2.setOnClickListener(cl);

        btn3.setOnClickListener(cl);
        };
    }
}

我想做类似计算 单击该按钮,然后出现在editText

3 个答案:

答案 0 :(得分:2)

我认为您代码中的问题是您在要创建的cl对象范围内调用了setOnClickListener函数,因此请尝试将代码修改为此:

public class MainActivity extends AppCompatActivity {

private Button btn1, btn2, btn3, btnC;
private EditText edit;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    edit = (EditText)findViewById(R.id.editText);
    btn1 = (Button)findViewById(R.id.btnOne);
    btn2 = (Button)findViewById(R.id.btnTwo);
    btn3 = (Button)findViewById(R.id.btnThree);
    btnC = (Button)findViewById(R.id.btnClear);


    View.OnClickListener cl = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == btn1)
                edit.setText(edit.getText().toString() + 1);
            else if (v == btn2)
                edit.setText(edit.getText().toString() + 2);
            else if (v == btn3)
                edit.setText(edit.getText().toString() + 3);
            else if (v == btnC)
                edit.setText("");
            }
        };

        btn1.setOnClickListener(cl);

        btn2.setOnClickListener(cl);

        btn3.setOnClickListener(cl);

    }
}

但是我建议您像这样使用Lambda表达式:

btn1.setOnClickListener(v -> edit.setText(edit.getText().toString() + 1));
btn2.setOnClickListener(v -> edit.setText(edit.getText().toString() + 2));
btn3.setOnClickListener(v -> edit.setText(edit.getText().toString() + 3));
btnC.setOnClickListener(v -> edit.setText(""));

这将更加清晰易读

答案 1 :(得分:1)

尝试一下,

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn1, btn2, btn3, btnC;
    private EditText edit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        edit = (EditText)findViewById(R.id.editText);
        btn1 = (Button)findViewById(R.id.btnOne);
        btn2 = (Button)findViewById(R.id.btnTwo);
        btn3 = (Button)findViewById(R.id.btnThree);
        btnC = (Button)findViewById(R.id.btnClear);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btnC.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btnOne:
                edit.setText(edit.getText().toString() + 1);
                break;
            case R.id.btnTwo:
                edit.setText(edit.getText().toString() + 2);
                break;
            case R.id.btnThree:
                edit.setText(edit.getText().toString() + 3);
                break;
            case R.id.btnClear:
                edit.setText("");
                break;
        }
    }
}

答案 2 :(得分:0)

最佳方法我始终使用以下代码。实施OnclickListener ,并覆盖onClick方法

公共类MainActivity扩展了AppCompatActivity实现的View.OnClickListener {

private Button b1, b2, b3, bClear;
private EditText txtCount;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txtCount= (EditText)findViewById(R.id.txtCount);
    b1 = (Button)findViewById(R.id.btn1);
    b2 = (Button)findViewById(R.id.btn2);
    b3 = (Button)findViewById(R.id.btn3);
    bClear = (Button)findViewById(R.id.btnClear);

    bt.setOnClickListener(this);
    b2.setOnClickListener(this);
    b3.setOnClickListener(this);
    bCount.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.btn1:
            txtCount.setText(edit.getText().toString() + 1);
            break;
        case R.id.btn2:
            txtCount.setText(edit.getText().toString() + 2);
            break;
        case R.id.btn3:
            txtCount.setText(edit.getText().toString() + 3);
            break;
        case R.id.bClear:
            txtCount.setText("");
            break;
    }
}

}