简体   繁体   English

Android - 选中单选按钮后自动更改LinearLayout可见性

[英]Android - Change LinearLayout visibility automatically after radio button is checked

Currently, my linearlayout is set as GONE in my main.xml . 目前,我的linearlayoutmain.xml设置为GONE I need to set the visibility of my linearlayout to visibile automatically once a radio button is checked. 检查单选按钮后,我需要将linearlayout的可见性设置为自动可见。 But having error force close. 但有错误力量关闭。 Is there anything wrong with my codes? 我的代码有什么问题吗?

calc.java calc.java

public class calc extends Activity implements OnClickListener{

EditText et;
RadioButton yes;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button submit =(Button)findViewById(R.id.submit);
    submit.setOnClickListener(this);

    final View linear = (EditText)findViewById(R.id.linear);
    yes = (RadioButton)findViewById(R.id.option1);
    yes.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {

        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {

            if(yes.isChecked())
                linear.setVisibility(View.VISIBLE);

        }
    });
}

public void onClick(View arg0) {


}

} }

main.xml main.xml中

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

<RadioGroup
android:id="@+id/radioG"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerHorizontal="true">

<RadioButton 
android:checked="false"
android:id="@+id/option" 
android:text="No"/>

<RadioButton 
android:checked="false"
android:id="@+id/option1" 
android:text="Yes"/>

</RadioGroup>

<LinearLayout
android:id="@+id/linear"
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/radioG"
android:visibility="gone">

<TextView  
android:id="@+id/edit"
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:text="Please enter something: "
/>

<EditText  
android:id="@+id/edit"
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:hint="numeric digits please"
/>
</LinearLayout>

<Button  
android:id="@+id/submit"
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/linear"
android:text="Submit"
/>   

ERROR Message 错误信息

Unable to start activity ComponentInfo{calc.calc/calc.calc.calc}: java.lang.ClassCastException: android.widget.LinearLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)

You are casting your LinearLayout into an EditText here: 您将LinearLayout转换为EditText

final View linear = (EditText)findViewById(R.id.linear);

Remove the cast and it should work. 删除演员,它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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