返回android中单选按钮的值?

时间:2014-04-17 11:31:22

标签: android

到目前为止我已经这样做了:

public void addListenerOnButton() {
    btnDisplay= (Button) findViewById(R.id.btnDisplay);

    // TODO has the search value entered by user and mentions the type of search by location,name and speciality 
    //radioSearchType= (RadioGroup) findViewById (R.id.radioSearchDoctor);
    //int selectedId = radioSearchType.getCheckedRadioButtonId();
    //radioSearchButton= (RadioButton) findViewById(selectedId);
    btnDisplay.setonClickListener(new OnClickListener(){
    if(radioSearchType.getCheckedRadioButtonId()!=-1){
        int id= radioSearchType.getCheckedRadioButtonId();
        View radioSearchButton = radioSearchType.findViewById(id);
        int radioId = radioSearchType.indexOfChild(radioSearchButton);
        RadioButton btn = (RadioButton) radioSearchType.getChildAt(radioId);
        String selection = (String) btn.getText();
    }

    Toast.makeText(SearchADoctor.this, radioSearchButton.getText(), Toast.LENGTH_SHORT);
});
}

但是它给了我一个错误,请让我知道我做错了什么?

2 个答案:

答案 0 :(得分:0)

您需要发布您的logcat才能获得帮助。我不知道您收到了什么错误我发现了您发布的代码中的两个错误。

将代码置于onClick方法中,并调用show()方法显示吐司。

btnDisplay.setOnClickListener(new OnClickListener() {
    public void onClick(View v)
    {
        if(radioSearchType.getCheckedRadioButtonId()!=-1){
        int id= radioSearchType.getCheckedRadioButtonId();
        View radioSearchButton = radioSearchType.findViewById(id);
        int radioId = radioSearchType.indexOfChild(radioSearchButton);
        RadioButton btn = (RadioButton) radioSearchType.getChildAt(radioId);
        String selection = (String) btn.getText();
    }

    Toast.makeText(SearchADoctor.this, radioSearchButton.getText(), Toast.LENGTH_SHORT).show();
    } 
});

答案 1 :(得分:0)

试试这个..

btnDisplay.setOnClickListener(new OnClickListener() {
    public void onClick(View v)
    {
        if(radioSearchType.getCheckedRadioButtonId()!=-1){
               int id= radioSearchType.getCheckedRadioButtonId();
               RadioButton btn = (RadioButton) radioSearchType.findViewById(id);
               String selection = (String) btn.getText();

               Toast.makeText(SearchADoctor.this, selection, Toast.LENGTH_SHORT).show();
        }
    } 
});