【问题标题】:Why does my onCheckedChanged method not get fired when i click a different radio button in my radio group?为什么当我单击单选组中的其他单选按钮时我的 onCheckedChanged 方法不会被触发?
【发布时间】:2012-03-11 19:25:16
【问题描述】:
package matthew.datacollector;

import android.app.Activity;
import android.view.View;
import android.widget.TextView;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.os.Bundle;

public class DataCollectorActivity extends Activity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {

    private TextView tv;
    private RadioGroup rg;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.tv = (TextView) findViewById(R.id.textView1);
        this.rg = (RadioGroup) findViewById(R.id.radioGroup1);
        rg.setOnClickListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        this.tv.setText("lol");
        Toast.makeText(this.getApplicationContext(), "hey", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onClick(View v) {
        this.tv.setText("lol");
    }
}

我不明白为什么这不起作用:/。我希望它设置 textview 的文本,你甚至可以看到我已经为调试举杯。仍然没有,我也试过没有@Overrides

【问题讨论】:

    标签: android radio-button radio-group


    【解决方案1】:

    您忘记在 RadioGroup 上设置监听器 onCheckedChange:

    rg.setOnCheckedChangeListener(this);
    

    【讨论】:

    • 啊,好尴尬!谢谢。
    猜你喜欢
    • 2018-10-19
    • 2023-02-03
    • 1970-01-01
    • 1970-01-01
    • 2020-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-02
    相关资源
    最近更新 更多