简体   繁体   中英

How to check the selected radiobutton?

I try to check the selected radio button,

I found a little detail, but I can't paste it.

Now i have one (View View ) error.

The program don't know which one was selected ,

and i can't find the solution.

What can i write more ?

Please help.

Thank you : ant Z picture from the problem

My code:

package myapplication.Buttons;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class MainActivity extends AppCompatActivity {
EditText editText;     
    
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EditText textView = findViewById(R.id.text);
        Button button = findViewById(R.id.button);
        Button btn1 = (Button)findViewById(R.id.button);
        Button btn2 = (Button)findViewById(R.id.exit);
       editText = findViewById(R.id.text);
        RadioGroup radioGroup =  findViewById(R.id.radioGroup1);

        editText.addTextChangedListener(new TextWatcher() {            
            //do something  }           
            });

        public void getSelectedRadioButton(View View ) {        <<< fail here :(View View )
            final RadioGroup radGroup = findViewById(R.id.radiogroup1);
            int radioID = radGroup.getCheckedRadioButtonId();
            RadioButton singleButton = findViewById(radioID);
        }
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RadioGroup radioGroup = findViewById(R.id.radiogroup1); 

                                 <<< if i push the btn1 the program don't know the radioID>>>

                if (radioGroup.getCheckedRadioButtonId() == R.id.radioButton_green) {
                //do something
                }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // do something
        }
    } );
    }}

You should work on 2 things to get this fixed.

  1. Make the correction of case sensitivity and correct View View as View view

  2. Move the function below the onCreate function and inside the MainActivity.

  3. You don't actually need 1&2 to get what you desire.

  4. You can copy-paste this code and get working.

Code:

package myapplication.Buttons;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.uploadedlobster.PwdHash.R;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class MainActivity extends AppCompatActivity {
    EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EditText textView = findViewById(R.id.text);
        Button button = findViewById(R.id.button);
        Button btn1 = (Button)findViewById(R.id.button);
        Button btn2 = (Button)findViewById(R.id.exit);
        editText = findViewById(R.id.text);
        RadioGroup radioGroup =  findViewById(R.id.radioGroup1);

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (radioGroup.getCheckedRadioButtonId() == R.id.radioButton_green) {
                    //do something
                }
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // do something
            }
        } );
    }
}

Happy Coding! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM