2013-04-24 58 views
0

我有一个包含两个RadioButton的RadioGroup的自定义对话框。选中的RadioButton决定了要设置的字符串。当我为什么我的RadioGroup返回'false'

Toast.makeText(NewFile.this, checkedId, Toast.LENGTH_LONG) 
          .show(); 

它显示checkedId为假而不管哪个单选按钮被选择,并且仅所述第二壳体设置字符串到正确的值。下面是我的代码目前:

AlertDialog.Builder customDialog = new AlertDialog.Builder(NewFile.this); 
     LayoutInflater layoutInflater = (LayoutInflater) getApplicationContext() 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View view = layoutInflater.inflate(R.layout.jquery_dialog, null); 
     final EditText idTxt = (EditText) view.findViewById(R.id.idName); 
     final CheckBox headerChk = (CheckBox) view.findViewById(R.id.headerChk); 
     final CheckBox footerChk = (CheckBox) view.findViewById(R.id.footerChk); 
     final RadioGroup group = (RadioGroup) view 
       .findViewById(R.id.jqmNavigation); 
     customDialog.setView(idTxt); 
     customDialog.setView(headerChk); 
     customDialog.setView(footerChk); 
     group.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(RadioGroup group, int checkedId) { 

       switch (checkedId) { 
       case R.id.ol: // First RadioButton 
        Toast.makeText(NewFile.this, checkedId, Toast.LENGTH_LONG) 
          .show(); 
        jqMobNavbar = "<div data-role=\"navbar\">\n" + "  <ol>\n" 
          + "   <li><a href=\"#\">Page 1</a></li>\n" 
          + "   <li><a href=\"#\">Page 2</a></li>\n" 
          + "   <li><a href=\"#\">Page 3</a></li>\n" 
          + "  <ol>\n" + " </div>\n"; 
        break; 
       case R.id.ul: // Second RadioButton 
        Toast.makeText(NewFile.this, checkedId, Toast.LENGTH_LONG) 
          .show(); 
        jqMobNavbar = "<div data-role=\"navbar\">\n" + "  <ul>\n" 
          + "   <li><a href=\"#\">Page 1</a></li>\n" 
          + "   <li><a href=\"#\">Page 2</a></li>\n" 
          + "   <li><a href=\"#\">Page 3</a></li>\n" 
          + "  <ul>\n" + " </div>\n"; 
        break; 

       } 

      } 
     }); 

和我的XML

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

    <com.androidwebtoolkit.TransparentPanel 
     android:id="@+id/transparentPanel1" 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:layout_weight="0" 
     android:paddingBottom="10dp" 
     android:paddingTop="10dp" > 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true" 
      android:layout_toRightOf="@+id/imageView1" 
      android:text="jBuilder" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 

     <ImageView 
      android:id="@+id/imageView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentLeft="true" 
      android:layout_marginLeft="11dp" 
      android:src="@drawable/jquerymobileicon" /> 

    </com.androidwebtoolkit.TransparentPanel> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dp" 
     android:layout_marginTop="10dp" 
     android:layout_weight="0" 
     android:text="ID:" 
     android:textSize="25sp" /> 

    <EditText 
     android:id="@+id/idName" 
     android:layout_width="160dp" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dp" 
     android:ems="10" > 

     <requestFocus /> 
    </EditText> 

    <CheckBox 
     android:id="@+id/headerChk" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dp" 
     android:layout_marginTop="10dp" 
     android:layout_weight="0" 
     android:text="Header" /> 

    <CheckBox 
     android:id="@+id/footerChk" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="10dp" 
     android:layout_marginTop="10dp" 
     android:layout_weight="0" 
     android:text="Footer" /> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
     android:paddingLeft="10dp" 
     android:text="Navigation" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <RadioGroup 
     android:id="@+id/jqmNavigation" 
     android:layout_width="312dp" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <RadioButton 
      android:id="@+id/ol" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Ordered List" /> 

     <RadioButton 
      android:id="@+id/ul" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Unordered List" /> 

    </RadioGroup> 

</LinearLayout> 

我也试过

group.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(RadioGroup group, int checkedId) { 
       int radioButtonID = group.getCheckedRadioButtonId(); 
       View radioButton = group.findViewById(radioButtonID); 
       int idx = group.indexOfChild(radioButton); 
       switch (idx) { 
       case 1: 
        Toast.makeText(NewFile.this, checkedId, Toast.LENGTH_LONG) 
          .show(); 
        jqMobNavbar = "<div data-role=\"navbar\">\n" + "  <ol>\n" 
          + "   <li><a href=\"#\">Page 1</a></li>\n" 
          + "   <li><a href=\"#\">Page 2</a></li>\n" 
          + "   <li><a href=\"#\">Page 3</a></li>\n" 
          + "  <ol>\n" + " </div>\n"; 
        break; 
       case 2: 
        Toast.makeText(NewFile.this, checkedId, Toast.LENGTH_LONG) 
          .show(); 
        jqMobNavbar = "<div data-role=\"navbar\">\n" + "  <ul>\n" 
          + "   <li><a href=\"#\">Page 1</a></li>\n" 
          + "   <li><a href=\"#\">Page 2</a></li>\n" 
          + "   <li><a href=\"#\">Page 3</a></li>\n" 
          + "  <ul>\n" + " </div>\n"; 
        break; 

       } 

      } 
     }); 

什么是检查单选按钮的选择值的正确方法是什么?

+0

你是什么意思的“它显示checkedId为假”? checkedId是一个整数,(在你的情况)它应该是R.id.ol或R.id.ul – ValarDohaeris 2013-04-24 21:08:18

回答

1

你是什么意思checkedId是错误的?这是一个int,不能是假的。如果你的任何一个case语句有效,那就意味着checkedId是它应该是的id值。

如果您将第一个RadioButton设置为XML格式,则在按下其他按钮之前,您的任何一种情况都不会被调用。或者,您也可以在每个按钮上设置:http://developer.android.com/reference/android/widget/CompoundButton.OnCheckedChangeListener.html

+0

'假'我的意思是Toast.makeText(NewFile.this,checkedId,Toast.LENGTH_LONG ) .show();是一个吐司显示'假' – RapsFan1981 2013-04-24 22:45:19

+0

我认为这是一个Eclipse问题。重新启动和项目干净得到它使用开关(checkedId)工作我首先尝试 – RapsFan1981 2013-04-24 23:33:52

+0

很高兴你得到它的工作。好Eclipse'。我无法告诉您在清理/重新启动时我解决了多少问题。 – KMDev 2013-04-26 02:06:57

0

它返回false,因为它的Integer。尝试使用:

Toast.makeText(getApplicationContext(), Integer.toString(checkedId), Toast.LENGTH_SHORT).show();