简体   繁体   English

如何在Android Studio上选择单选按钮

[英]How to choose Radio Button on android studio

I want to make a radiobutton, which a radiobutton selected, a edit text is appears. 我要制作一个单选按钮,选中该单选按钮,将显示一个编辑文本。

For example, a have a 2 radio button 例如,有一个2单选按钮

A. two Edittext appears
B. three edittext appears

how i make this? 我怎么做的?

    public void onRadioButtonClicked(View view) {
    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();

    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.radio_pirates:
            if (checked)
                // Pirates are the best
            break;
        case R.id.radio_ninjas:
            if (checked)
                // Ninjas rule
            break;
    }
}

ready to 3 EditText 准备好3 EditText

    EditText editTextA = null;
    EditText editTextB = null;
    EditText editTextC = null;

and when choose A button code: 当选择一个按钮代码时:

    editTextA.setVisibility(View.VISIBLE);
    editTextB.setVisibility(View.VISIBLE);
    editTextC.setVisibility(View.GONE);

and when choose B button code: 当选择B按钮代码时:

    editTextA.setVisibility(View.VISIBLE);
    editTextB.setVisibility(View.VISIBLE);
    editTextC.setVisibility(View.VISIBLE);

when when you choose A button it will just show editTextA,B and when you choose B, all 当您选择A按钮时,它只会显示editTextA,B,而当您选择B时,所有

use radioGroup over radioButton and than use setOnCheckedChangeListener for that radioGroup and you will get checkedId in listener. radioButton使用radioGroupradioButton对该radioGroup使用setOnCheckedChangeListener,您将在侦听器中获得checkedId。

.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged (RadioGroup group,int checkedId){

            Log.d("chk", "id" + checkedId);

            if (checkedId == R.id.a) {
                //some code
            } else if (checkedId == R.id.b) {
                //some code
            }
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Java Android选择单选按钮值? - How to Choose Radio Button Value using Java Android? 我如何知道在Android Studio中选择了哪个单选按钮 - How can I know which radio button is selected in Android studio 如何在Android Studio中设置和获取性别单选按钮的值? - how to set and get the value of gender radio button in android studio? 如何在 android studio 中动态更改单选按钮的名称? - How to change name of Radio button dynamically in android studio? 如何在 android 工作室中保存选中的单选按钮的 state - How to save state of checked radio button in android studio 关闭应用程序后如何保持选中单选按钮 - android studio - How to keep a radio button checked after closing the application - android studio 如何在android studio中从意图设置单选按钮检查 - how to set radio button check from intent in android studio 带有单选按钮值的 Android Studio Listview - 如何检查单选按钮并获取值? - Android Studio Listview with radio button value - How to check radio button and get value? 单选按钮更改布局活动Android Studio - Radio Button change layout activity Android Studio Android Studio 中单选按钮的 onClick() 替代选项 - Alternate option for onClick() for Radio Button in Android Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM