android radiobutton按钮接受

时间:2015-04-16 11:28:49

标签: java android android-layout

按钮Set Color出现问题。 Set Color将激活所选的RadioButton。例如,选择第一个单选按钮,单击“设置颜色”时,背景颜色变为红色。这是我的Android Studio代码。

更新

我的应用程序正在运行:

当我在第一个RadioButton上点击时,我的textview背景颜色会自动更改。它跳过我的“设置颜色”按钮。

我的应用程序正在运行。

当我在第一个RadioButton上点击时,我接受我的选择点击“设置颜色”按钮我的textview背景颜色变化。

XML文件

<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文件

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);
    }
}

3 个答案:

答案 0 :(得分:0)

创建用于保存textview颜色的字段,并在onRadioClicked上保存新值。点击你的&#34;设置颜色&#34;给你的textView.setBackgroundColor(savedColor)打电话。

答案 1 :(得分:0)

尝试以下

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);

        }
    });
}

并且不要忘记从xml中的单选按钮中删除android:onClick="onRadioClicked"

答案 2 :(得分:0)

您可以通过

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

现在你可以做你想做的事 希望这有帮助