通过按钮从主布局过渡似乎破坏了第二个布局中的单选按钮



完全披露为了反映问题的实质,我改了题目

不知怎么的,我似乎很笨…所以我做错的地方与我在屏幕之间切换的方式有关。当我所做的代码已经显示为主要活动,一切似乎运行良好。所以它和这个有关系

        buttonArea.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            v.setVisibility(View.GONE);
            setContentView(R.layout.activity_area);
        }
    });

关于为什么一般情况下单选按钮不能工作的旧问题信息

我在过去做过一些Android,我不知道为什么我在做什么不工作。这应该很简单……但由于某些原因,它不起作用。

我应该说没有错误显示,所以所有内容都导入了。我正在使用Android Studio.

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_area);

    final RadioButton rb1 =(RadioButton)findViewById(R.id.radFeet);
    final RadioButton rb2 =(RadioButton)findViewById(R.id.radMeters);
    final EditText txt = (EditText)findViewById(R.id.txtAreaIn);
    final RadioGroup rg = (RadioGroup)findViewById(R.id.radGroupArea);
    rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            double distance= Double.parseDouble(txt.getText().toString());
            if(rb1.isChecked()){
                txt.setText(String.valueOf(distance/con));
            }
            else if(rb2.isChecked()){
                txt.setText(String.valueOf(distance*con));
            }
        }
    });

}

这是活动

的XML
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="edu.gatech.seclass.converter.Area"
android:id="@+id/actArea">
<TextView android:text="@string/title_activity_area" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/lblArea"
    android:textSize="45dp"
    android:layout_alignParentEnd="true"
    android:layout_alignParentStart="true" />
<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"
    android:ems="10"
    android:id="@+id/txtAreaIn"
    android:layout_below="@+id/lblArea"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="72dp" />

<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignEnd="@+id/lblArea"
    android:layout_below="@+id/txtAreaIn"
    android:minHeight="55dp"
    android:minWidth="55dp"
    android:id="@+id/radGroupArea">
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Conv_to_feet"
        android:id="@+id/radFeet"
        android:checked="false"
        android:layout_below="@+id/txtAreaIn"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Conv_to_meters"
        android:id="@+id/radMeters"
        android:layout_below="@+id/radFeet"
        android:layout_centerHorizontal="true"
        android:checked="false"
        />
</RadioGroup>

然而,当我点击任何一个单选按钮…什么都没发生。我是不是漏掉了什么简单的东西?

可能是android:checked="false"引起的问题。你能试着去掉它,看看效果如何吗?

所以我发现,而不是切换活动的方式,我需要这样做。

startActivity(new Intent(MainActivity.this, Area.class));

虽然慢,但它完成了这个简单的任务。仍然很好奇为什么前面的代码将导航到正确的布局,但什么也不做......现在我想起来可能是因为类没有改变