单选按钮启用错误 [英] Radio button enable errror

查看:66
本文介绍了单选按钮启用错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生,

i创建了两个活动,在点击一个单选按钮后,它应该进入第二个活动。一切正常。它进入下一个活动,但我遇到了一个小问题。点击一个单选按钮后如果我试图点击另一个单选按钮,则第一个单选按钮仍然显示已选中..单击另一个或相同后,所有单选按钮显示已选中。

这里是我的代码,

第一个XML文件:activity_main.xml

hi sir,
i have created two activities in which after clicking on one radio button it should go to second activity.Everything works fine.It goes to the next activity but i got a little problem.After clicking one radio button if i am trying to click another radio button then the first radio button is still showing selected..and all the radio button is showing selected after clicking another or same.
here is my code,
First XML file:activity_main.xml

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android">
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <textview>
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select Branch"/>

    <radiobutton>
        android:id="@+id/radioIT"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Information Technology"
        android:checked="true" />

    <radiobutton>
        android:id="@+id/radioCS"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Computer Science" />

    <Button

        android:id="@+id/buttonok"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="ok" />

</radiobutton></radiobutton></textview></linearlayout>





第二个XML文件:sem.Xml





Second XML file:sem.Xml

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android">
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <textview>
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select Semister" />


    <radiobutton>
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sem 1"
        android:checked="true" />

    <radiobutton>
        android:id="@+id/radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="sem 2" />

    <radiobutton>
        android:id="@+id/radioButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="sem 3" />

    <radiobutton>
        android:id="@+id/radioButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="sem 4" />

</radiobutton></radiobutton></radiobutton></radiobutton></textview></linearlayout><pre>

First class File:(MainActivity.java)
<pre>package com.radiobutton;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;

public class MainActivity extends Activity {
	RadioButton search,type;
	Button Ok;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		search = (RadioButton) findViewById(R.id.radioIT);
		type = (RadioButton) findViewById(R.id.radioCS);

		  Ok = (Button) findViewById(R.id.buttonok);
		  Ok.setOnClickListener(new View.OnClickListener() {
		        @Override
		        public void onClick(View v) {
		        if(search.isChecked()){
		            Intent createIntent = new Intent(getApplicationContext(), Semister.class); // <----- START "SEARCH" ACTIVITY

		            startActivity(createIntent);

		            

		            }

		            else

		            {

		              if(type.isChecked())

		              {

		              Intent typeIntent = new Intent(getApplicationContext(), Semister.class); // <----- START "TYPE ENTRIES OUT" ACTIVITY

		              startActivity(typeIntent);

		              }

		            }

		        }

		    });

	}





}



第二类文件:(Semister.java)


second class file:(Semister.java)

package com.radiobutton;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;

public class MainActivity extends Activity {
	RadioButton search,type;
	Button Ok;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		search = (RadioButton) findViewById(R.id.radioIT);
		type = (RadioButton) findViewById(R.id.radioCS);

		  Ok = (Button) findViewById(R.id.buttonok);
		  Ok.setOnClickListener(new View.OnClickListener() {
		        @Override
		        public void onClick(View v) {
		        if(search.isChecked()){
		            Intent createIntent = new Intent(getApplicationContext(), Semister.class); // <----- START "SEARCH" ACTIVITY
		            startActivity(createIntent);
		            
		            }
		            else
		            {
		              if(type.isChecked())
		              {
		              Intent typeIntent = new Intent(getApplicationContext(), Semister.class); // <----- START "TYPE ENTRIES OUT" ACTIVITY
		              startActivity(typeIntent);
		              }
		            }
		        }
		    });
	}


}

推荐答案

你必须将单选按钮分组到一个广播组如下例所示:





Android Radio Group示例
You have to group the radio buttons into a radio group like the following example:


Android Radio Group Example


这篇关于单选按钮启用错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆