2013-01-21 29 views
0

我在xml中擁有RadioGroup。我用代碼中的RadioButton填充它。 問題是,在模擬器RadioButtons不行爲w.r.t RadioGroup。我可以一次選擇所有的單選按鈕。但是,一切似乎都能在真實設備上正常工作。Android RadioButtons僅在仿真器上行爲不端

下面是截圖和代碼的所有文件:

從模擬器截圖

Screenshot from emulator

來自設備的三星Galaxy

Screenshot from device Samsung galaxy

截圖

來源爲MainActivity.java

public class MainActivity extends Activity { 
/** Called when the activity is first created. */ 
private RadioGroup rg; 

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

    LayoutInflater inflater = LayoutInflater.from(getBaseContext()); 
    rg = (RadioGroup)findViewById(R.id.rg); 

    for (int i = 0; i < 5; i++) { 
     inflater.inflate(R.layout.myradio, rg,true); 
    } 

} 

}

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:id="@+id/llContainer" 
    tools:context=".MainActivity" > 


<RadioGroup 
    android:id="@+id/rg" 
    android:background="#0000ff" 
    android:orientation="vertical" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent"> 

</RadioGroup> 

myradio.xml

<?xml version="1.0" encoding="utf-8"?> 

<RadioButton xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:text="RadioButton" /> 
+0

@Avadhani BTW,佈局是不是我關心的,但行爲。 – Avinazz

回答

0

它會像這樣

public class example extends Activity { 
    /** Called when the activity is first created. */ 
    private RadioGroup rgroup; 
    RadioButton rb; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.example); 


     rgroup = (RadioGroup)findViewById(R.id.rg); 
     final RadioButton[] rb = new RadioButton[5]; 
     for (int i = 0; i < 5; i++) { 
      rb[i]=new RadioButton(this); 
      rb[i].setText("rdo"+i); 
      rb[i].setId(i); 
      rgroup.addView(rb[i]); 


     } 

    } 

    } 

enter image description here

+0

我不想更改創建佈局的方法。因爲我的需求是動態的。另外,你可能已經注意到這個代碼在設備上正常工作。 – Avinazz

+0

@Avinazz .k ...我會嘗試不同的方式 – Janmejoy

+0

我沒有任何問題用任何方法生成此佈局,但它必須是動態的。我可以用幾種不同的方式做同樣的事情,但模擬器仍然讓我失望。順便說一句,從2年開始在Android上工作,我仍然犯了愚蠢的錯誤,這在我看來並不是這樣。 – Avinazz