简体   繁体   中英

android radiobutton button accept

I have a problem with button Set Color . Set Color is to activate the selected RadioButton . For example, selecting the first radiobutton and when you click "Set Color" is the background color change to red. Here is my Android Studio codes.

UPDATE

My app working process now:

When I click for example on first RadioButton my textview background color change automatically. Its skipping my "Set Color" button.

My intended app working.

When I click for example on first RadioButton and after that I accept my choose clicking "Set Color" button my textview background color change.

XML file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:orientation="vertical">


    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:checkedButton="@+id/czerwonyguzik"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/czerwonyguzik"
            android:layout_width="wrap_content"

            android:layout_height="wrap_content"
            android:background="@color/Czerwony"
            android:onClick="onRadioClicked" />

        <RadioButton
            android:id="@+id/bialyguzik"
            android:layout_width="wrap_content"

            android:layout_height="wrap_content"
            android:background="@color/White"
            android:onClick="onRadioClicked" />

        <RadioButton
            android:id="@+id/niebieskiguzik"
            android:layout_width="wrap_content"

            android:layout_height="wrap_content"
            android:background="@color/Niebieski"
            android:onClick="onRadioClicked" />

    </RadioGroup>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="end"
        android:orientation="horizontal">

        <Button
            android:id="@+id/setcolor"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:text="Set Color" />

        <Button
            android:id="@+id/cancel"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:onClick="anulacjaopcji"
            android:text="Anuluj" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textwju"
            android:layout_width="match_parent"
            android:layout_height="101dp"
            android:background="@color/red"
            android:textColor="@color/White" />


    </LinearLayout>


</LinearLayout>

JAVA file

public class LinearLayout extends ActionBarActivity {

    RadioGroup rg;
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_linear_layout);
        TextView textView = (TextView) findViewById(R.id.textwju);
        rg = (RadioGroup) findViewById(R.id.radioGroup);


    }

    public void onRadioClicked(View view) {
        textView = (TextView) findViewById(R.id.textwju);

        // czy guzik jest wcisniety
        boolean wcisniety = ((RadioButton) view).isChecked();

        //sprawdzenie ktory guzik jest wcisniety
        switch (view.getId()) {
            case R.id.czerwonyguzik:
                if (wcisniety)
                    textView.setBackgroundColor(getResources().getColor(R.color.Czerwony));

                break;
            case R.id.bialyguzik:
                if (wcisniety)
                    textView.setBackgroundColor(getResources().getColor(R.color.White));
                break;
            case R.id.niebieskiguzik:
                if (wcisniety)
                    textView.setBackgroundColor(getResources().getColor(R.color.Niebieski));
                break;
        }

    }

    public void anulacjaopcji(View view) {
        rg.check(R.id.czerwonyguzik);
        textView.setBackgroundColor(getResources().getColor(R.color.Czerwony));
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_radio_button, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Create field for saving textview color in, and just save in it new value on onRadioClicked . After you click your "Set Color" call your textView.setBackgroundColor(savedColor) .

Try the following

RadioButton radioButton1, radioButton2, radioButton3;
Button setColorButton, cancelButton;
TextView textwju;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.radiobutton_example);


    radioButton1   = (RadioButton) findViewById(R.id.czerwonyguzik);
    radioButton2   = (RadioButton) findViewById(R.id.bialyguzik);
    radioButton3   = (RadioButton) findViewById(R.id.niebieskiguzik);
    setColorButton = (Button) findViewById(R.id.setcolor);
    cancelButton = (Button) findViewById(R.id.cancel);
    textwju        = (TextView) findViewById(R.id.textwju);



    setColorButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(radioButton1.isChecked()) {
                textwju.setBackgroundColor(getResources().getColor(R.color.blue));
            } else if(radioButton2.isChecked()) {
                textwju.setBackgroundColor(getResources().getColor(R.color.white));
            } else if(radioButton3.isChecked()) {
                textwju.setBackgroundColor(getResources().getColor(R.color.red));
            }

        }
    });

    cancelButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            radioButton1.setChecked(true);

        }
    });
}

And don't forget to remove android:onClick="onRadioClicked" from the radio buttons in the xml.

you can get the option selected by

int radioButtonID = group.getCheckedRadioButtonId();
View radioButton = group.findViewById(radioButtonID);
int radioId = group.indexOfChild(radioButton);
String optionText = 
(RadioButton)group.getChildAt(radioId)).getText().toString();

now you can do what ever you want hope this helps

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