Android - 选中单选按钮后自动更改LinearLayout可见性
问题描述:
目前,我的linearlayout
在我的main.xml
中设置为GONE
。一旦单选按钮被选中,我需要将我的linearlayout的可见性设置为visibile。但有错误的力量关闭。我的代码有什么问题吗?Android - 选中单选按钮后自动更改LinearLayout可见性
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中
<?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消息
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)
答
您铸造你的LinearLayout
为EditText
这里:
final View linear = (EditText)findViewById(R.id.linear);
取出演员和它应该工作。
哈哈。我没有意识到这一点。谢谢。它正在工作。 :D – Hend 2011-12-22 03:57:26
请关闭此问题。 – 2011-12-22 04:01:26