0

我想EditText在单选按钮时获取字符串中的值setOnCheckedChangeListener(false)并这样做

String  strmusic = "MyString";
EdiText interst_edt;

rdbtn_music.setOnCheckedChangeListener(new OnCheckedChangeListener() {          
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {            

                if(rdbtn_music.isChecked()){                            
                    interst_edt.setText(strmusic.toString());
                    Log.i("HotSpot", "Interest strmusic values-"+strmusic +""+isChecked);                       
                }else{
                     strmusic = interst_edt.getText().toString();                   
                    Log.i("HotSpot", "Interest strmusic values-"+strmusic +""+isChecked);
                }
            }
        });

但问题是`strmusic 字符串值未使用Edittext 中的新给定字符串更新,我做错了请向我提问。

4

2 回答 2

1

我得到了你的问题的解决方案。尝试执行以下代码。

    RadioButton radioButton;
    EditText editText;

    String strmusic = "MyString";
    boolean BOOL = false;

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

        radioButton = (RadioButton) findViewById(R.id.radioButton1);
        editText = (EditText) findViewById(R.id.editText1);


        radioButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(v.getId()==radioButton.getId() && BOOL){
                    radioButton.setChecked(false);
                }else{
                    BOOL=true;
                }
            }
        });

        radioButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {

                Log.i("HotSpot", "Interest strmusic values- Before" + strmusic
                        + "" + isChecked);
                if (isChecked) {
                /*  Log.i("HotSpot", "Inside true");
                    editText.setText(strmusic.toString());
                    strmusic = editText.getText().toString();
                    buttonView.setChecked(false);
                    Log.i("HotSpot", "Interest strmusic values-" + strmusic
                            + "" + isChecked);*/
                    BOOL=false;
                } else {
                    Log.i("HotSpot", "Inside False");
                    strmusic = editText.getText().toString();
                    Log.i("HotSpot", "After Radio Button False Value of strmusic:" + strmusic
                            + "");
//                  buttonView.setChecked(true);

                }
            }
        });
    }
于 2012-11-08T08:55:40.700 回答
1

当 onclick 中单选按钮的状态发生变化时,将调用 setOnCheckedChangeListener。检查它是否处于 false 状态。

并确保你初始化了你的edittext

EdiText interst_edt = (EditText) findViewById(R.id.edittext);

于 2012-11-08T07:49:19.070 回答