RadioButton在布局之间切换

时间:2016-02-19 11:57:50

标签: java android

所以我想在我的应用中使用计算器。用户将首先点击RadioButton,它将用于Imperial和Metric单位,并且根据用户点击的那个,将出现其中一个布局。

如你所知,英制单位有英尺和英寸,我需要两个EditTexts,但公制单位只需要一个。

这就是我想要的,在相同活动中的布局之间切换以避免使用碎片。

有什么想法吗?

修改

xml文件

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="10dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="10dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="hr.app.liftme.liftmehr.KalkulatoriBFMan"
        tools:showIn="@layout/activity_kalkulatori_bfman"
        android:orientation="vertical">


        <android.support.v7.widget.CardView
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:foreground="?android:attr/selectableItemBackground"
            android:clickable="false"
            android:id="@+id/Osnove5"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            card_view:cardCornerRadius="2dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_weight="1"
            android:elevation="50dp"
            card_view:cardBackgroundColor="#ffffff"
            android:layout_marginBottom="10dp">

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/linearLayout28"
                android:layout_marginBottom="20dp"
                android:layout_marginTop="55dp"
                android:padding="7dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="@string/bf_uvodno"
                    android:id="@+id/textView140"
                    android:textStyle="bold" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:text="@string/bf_tekst_upute"
                    android:id="@+id/textView141"
                    android:textColor="#424242" />

            </LinearLayout>
        </android.support.v7.widget.CardView>


        <android.support.v7.widget.CardView
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:foreground="?android:attr/selectableItemBackground"
            android:clickable="false"
            android:id="@+id/Drugo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            card_view:cardCornerRadius="2dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_weight="1"
            android:layout_marginBottom="10dp"
            android:elevation="50dp"
            card_view:cardBackgroundColor="#ffffff">

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/linearLayout27"
                android:layout_below="@+id/linearLayout28"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:padding="7dp">

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceLarge"
                        android:text="@string/bf_radio_naslov"
                        android:id="@+id/textView3"
                        android:textStyle="bold" />
                </LinearLayout>

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <RadioGroup
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                        <RadioButton
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:id="@+id/radioButtonBFZeneCmKg"
                            android:text="@string/bf_radio2"

                            android:onClick="radioZeneCmKgClicked"
                            android:checked="false" />

                        <RadioButton
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:id="@+id/radioButtonBFZeneFtLb"
                            android:text="@string/bf_radio1"
                            android:onClick="radioZeneBFFtLbClicked"
                            android:checked="false" />

                    </RadioGroup>
                </LinearLayout>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="@string/bf_edit_tezina"
                    android:id="@+id/textView133"
                    android:textStyle="bold" />

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="numberDecimal"
                    android:ems="10"
                    android:id="@+id/editTezinaBF"
                    android:hint="@string/bf_edit_tezina_hint_z"
                    android:layout_marginBottom="5dp" />

                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/LayoutVisinaMetric">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceLarge"
                        android:text="@string/bf_edit_visina"
                        android:id="@+id/textViewVisinaMetric"
                        android:textStyle="bold" />

                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:inputType="numberDecimal"
                        android:ems="10"
                        android:id="@+id/editVisinaBF"
                        android:hint="@string/bf_edit_visina_hint_z"
                        android:layout_marginBottom="5dp" />
                </LinearLayout>

                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/LayoutVisinaImperial">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceLarge"
                        android:text="@string/bf_feetinch_naslov"
                        android:id="@+id/textViewVisinaImperial"
                        android:textStyle="bold" />

                    <LinearLayout
                        android:orientation="horizontal"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                        <EditText
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:inputType="numberDecimal"
                            android:ems="10"
                            android:id="@+id/editTextVisinaFT"
                            android:layout_weight="1"
                            android:hint="@string/bf_radio1_hint1" />

                        <EditText
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:inputType="numberDecimal"
                            android:ems="10"
                            android:id="@+id/editTextVisinaINC"
                            android:layout_weight="1"
                            android:hint="@string/bf_radio1_hint2" />
                    </LinearLayout>
                </LinearLayout>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="@string/bf_edit_struk"
                    android:id="@+id/textView135"
                    android:textStyle="bold" />

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="numberDecimal"
                    android:ems="10"
                    android:id="@+id/editStrukBF"
                    android:hint="@string/bf_edit_struk_hint_z"
                    android:layout_marginBottom="5dp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="@string/bf_edit_bokovi"
                    android:id="@+id/textView136"
                    android:textStyle="bold" />

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="numberDecimal"
                    android:ems="10"
                    android:id="@+id/editBokoviBF"
                    android:hint="@string/bf_edit_bokovi_hint_z"
                    android:layout_marginBottom="5dp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="@string/bf_edit_vrat"
                    android:id="@+id/textView137"
                    android:textStyle="bold" />

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="numberDecimal"
                    android:ems="10"
                    android:id="@+id/editVratBF"
                    android:hint="@string/bf_edit_vrat_hint_z"
                    android:layout_marginBottom="5dp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="@string/bf_edit_godine"
                    android:id="@+id/textView138"
                    android:textStyle="bold" />

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="numberDecimal"
                    android:ems="10"
                    android:id="@+id/editGodineBF"
                    android:hint="@string/bf_edit_godine_hint_z"
                    android:layout_marginBottom="15dp" />

                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_below="@+id/linearLayout27"
                    android:layout_marginBottom="20dp"
                    android:id="@+id/linearLayout29">

                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/bf_btn_izracunaj"
                        android:id="@+id/btnIzracunajBF"
                        android:layout_gravity="center_horizontal"
                        android:background="#424242"
                        android:textColor="#ffffff"
                        android:layout_marginBottom="10dp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceLarge"
                        android:text="@string/bf_tekst_rezultat"
                        android:id="@+id/textView139"
                        android:layout_gravity="center_horizontal" />

                    <LinearLayout
                        android:orientation="horizontal"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:gravity="center_horizontal">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textAppearance="?android:attr/textAppearanceMedium"
                            android:id="@+id/textRezultatBF"
                            android:layout_gravity="center_horizontal" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textAppearance="?android:attr/textAppearanceMedium"
                            android:text="@string/bf_rezultat"
                            android:id="@+id/textView152" />
                    </LinearLayout>
                </LinearLayout>

            </LinearLayout>
        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:foreground="?android:attr/selectableItemBackground"
            android:clickable="false"
            android:id="@+id/Osnove3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            card_view:cardCornerRadius="2dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_weight="1"
            android:elevation="50dp"
            card_view:cardBackgroundColor="#ffffff">

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:layout_below="@+id/linearLayout27"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:padding="7dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="@string/bf_tekst_tumacenje"
                    android:id="@+id/textView142"
                    android:gravity="center_horizontal"
                    android:layout_marginBottom="5dp"
                    android:textStyle="bold" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:text="@string/bf_tumacenje_rezultat_z"
                    android:id="@+id/textView143"
                    android:gravity="center_horizontal"
                    android:textColor="#424242" />
            </LinearLayout>
        </android.support.v7.widget.CardView>
    </LinearLayout>
</ScrollView>

的java

public class KalkulatoriBFMan extends AppCompatActivity {

    EditText editTezina, editVisina, editStruk, editBokovi, editVrat, editGodine, editVisinaFT, editVisinaINC;
    Button btnIzracunajBF;
    TextView textRezultatBF, textVisinaMetric, textVisinaImperial;
    LinearLayout imperial, metric;

    public void radioZeneCmKgClicked(View view){
        boolean checked = ((RadioButton) view).isChecked();
        switch (view.getId()){
            case R.id.radioButtonBFZeneCmKg:
                if (checked)
                    editVisina.setVisibility(View.VISIBLE);
                imperial.setVisibility(View.GONE);
                btnIzracunajBF.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View v){
                        // IMPERIAL FORMULA
                        //Body Fat (women) = 495 / (1.29579 - .35004 * log10(Waist + Hip - Neck) + 0.22100 * log10(Height)) - 450
                        double prvoPolje = Double.parseDouble(editTezina.getText().toString());
                        double drugoPolje = Double.parseDouble(editVisina.getText().toString());
                        double trecePolje = Double.parseDouble(editStruk.getText().toString());
                        double cetvrtoPolje = Double.parseDouble(editBokovi.getText().toString());
                        double petoPolje = Double.parseDouble(editVrat.getText().toString());
                        double sestoPolje = Double.parseDouble(editGodine.getText().toString());

                        double rezultat = 0;

                        rezultat = 495 / (1.0324 - 0.19077 * Math.log10(trecePolje - petoPolje) + 0.15456 * Math.log10(drugoPolje)) - 450;
                        rezultat = Math.round(rezultat * 100.0) / 100.0;
                        textRezultatBF.setText(Double.toString(rezultat) + " %");
                    }
                });
                break;
        }
    }
    public void radioZeneBFFtLbClicked(View view) {
        boolean checked = ((RadioButton) view).isChecked();
        switch (view.getId()) {
            case R.id.radioButtonBFZeneFtLb:
                if (checked)
                    editVisinaFT.setVisibility(View.VISIBLE);
                metric.setVisibility(View.GONE);

                btnIzracunajBF.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View v){
                        // IMPERIAL FORMULA
                        //Body Fat (men) = 495 / (1.29579 - .35004 * log10(Waist - Neck) + 0.22100 * log10(Height)) - 450
                        double prvoPolje = Double.parseDouble(editTezina.getText().toString());
                        double drugoPolje = Double.parseDouble(editVisinaFT.getText().toString());
                        double trecePolje = Double.parseDouble(editStruk.getText().toString());
                        double cetvrtoPolje = Double.parseDouble(editBokovi.getText().toString());
                        double petoPolje = Double.parseDouble(editVrat.getText().toString());
                        double sestoPolje = Double.parseDouble(editGodine.getText().toString());
                        double sedmoPolje = Double.parseDouble(editVisinaINC.getText().toString());
                        double rezultat = 0;

                        rezultat = 495 / (1.29579 - 0.35004 * Math.log10(trecePolje - petoPolje) + 0.22100 * Math.log10(((drugoPolje * 12) + sedmoPolje))) - 450;
                        rezultat = Math.round(rezultat * 100.0) / 100.0;
                        textRezultatBF.setText(Double.toString(rezultat) + " %");
                    }
                });
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kalkulatori_bfwoman);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        Intent intent = getIntent();

        editTezina = (EditText) findViewById(R.id.editTezinaBF);
        editVisina = (EditText) findViewById(R.id.editVisinaBF);
        editStruk = (EditText) findViewById(R.id.editStrukBF);
        editBokovi = (EditText) findViewById(R.id.editBokoviBF);
        editVrat = (EditText) findViewById(R.id.editVratBF);
        editGodine = (EditText) findViewById(R.id.editGodineBF);
        editVisinaFT = (EditText) findViewById(R.id.editTextVisinaFT);
        editVisinaINC = (EditText) findViewById(R.id.editTextVisinaINC);
        textVisinaImperial = (TextView) findViewById(R.id.textViewVisinaImperial);
        textVisinaMetric = (TextView) findViewById(R.id.textViewVisinaMetric);
        imperial = (LinearLayout) findViewById(R.id.LayoutVisinaImperial);
        metric = (LinearLayout) findViewById(R.id.LayoutVisinaMetric);

        btnIzracunajBF = (Button) findViewById(R.id.btnIzracunajBF);

        textRezultatBF = (TextView) findViewById(R.id.textRezultatBF);





    }



}

请注意,我通过将可见性设置为GONE解决了空白问题,但是当我打开活动时,两个EditTexts都显示,英制和公制。我希望它们都是不可见的,直到用户选择一个RadioButton

1 个答案:

答案 0 :(得分:2)

要为按钮定义click事件处理程序,请将android:onClick属性添加到XML布局中的<RadioButton>元素。此属性的值必须是要响应click事件而调用的方法的名称。然后,托管布局的Activity必须实现相应的方法。

例如,这里有几个RadioButton对象:

<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <RadioButton android:id="@+id/radio_pirates"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/pirates"
        android:onClick="onRadioButtonClicked"/>
    <RadioButton android:id="@+id/radio_ninjas"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/ninjas"
        android:onClick="onRadioButtonClicked"/>
</RadioGroup>

在承载此布局的Activity中,以下方法处理两个单选按钮的单击事件:

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.imperial:
            if (checked)
                EditBox1.setVisibility(Visible)
            break;
        case R.id.metrics:
            if (checked){
                EditBox1.setVisibility(Visible)
                EditBox2.setVisibility(Visible) 
            }
            break;
    }
}

来源:http://developer.android.com/guide/topics/ui/controls/radiobutton.html