2016-09-05 13 views
0

テキストボックス内のラジオボタンのn個からラジオボタンのテキストを選択するラジオボタンを与えるカスタムダイアログボックスを作成しようとしています。これは私が試したものです。..getCheckedRadioButtonId()を介してラジオボタンのidを取得できません

]ダイアログボックスの[マイレイアウトが

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

<RadioGroup 
    android:id="@+id/rg_carType" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="10dp" 
    android:orientation="vertical"> 

    <RadioButton 
     android:id="@+id/rb_hatchD" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Hatchback(Diesel)" /> 

    <RadioButton 
     android:id="@+id/rb_hatchP" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Hatchback(Petrol)" /> 

    <RadioButton 
     android:id="@+id/rb_sedanD" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Sedan(Diesel)" /> 

    <RadioButton 
     android:id="@+id/rb_sedanP" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Sedan(Petrol)" /> 

    <RadioButton 
     android:id="@+id/rb_suv" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="SUV" /> 
</RadioGroup> 

<ImageView 
    android:id="@+id/iv_carType" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="20dp" 
    android:layout_toRightOf="@+id/rg_carType" 
    /> 

<Button 
    android:id="@+id/btn_confirmCar" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/rg_carType" 
    android:layout_centerHorizontal="true" 
    android:background="@drawable/button_2" 
    android:text="Confirm" 
    android:textColor="#fff" /> 

で、カスタムダイアログボックスの機能は、問題がスイッチケースということである

private void diplayCartype() { 
final Dialog dialog_car = new Dialog(FinalActivity.this); 
final RadioButton rb_select; 
RadioGroup rg_carType; 
ImageView iv_carType; 
Button btn_confirmCar; 
String sCar="?"; 
dialog_car.setTitle("Select Car Type"); 

dialog_car.setContentView(R.layout.dialog_cartype); 

iv_carType=(ImageView)dialog_car.findViewById(R.id.iv_carType); 
btn_confirmCar=(Button)dialog_car.findViewById(R.id.btn_confirmCar); 
rg_carType=(RadioGroup)dialog_car.findViewById(R.id.rg_carType); 

int selected=rg_carType.getCheckedRadioButtonId(); 
rb_select=(RadioButton)dialog_car.findViewById(selected); 

switch(selected) 
{ 
    case R.id.rb_hatchD: 
     iv_carType.setImageResource(R.drawable.hatch_d); 
     sCar=rb_select.getText().toString(); 
     break; 
    case R.id.rb_hatchP: 
     iv_carType.setImageResource(R.drawable.hatch_p); 
     sCar=rb_select.getText().toString(); 
     break; 
    case R.id.rb_sedanD: 
     iv_carType.setImageResource(R.drawable.sedan_d); 
     sCar=rb_select.getText().toString(); 
     break; 
    case R.id.rb_sedanP: 
     iv_carType.setImageResource(R.drawable.sedan_p); 
     sCar=rb_select.getText().toString(); 
     break; 
    case R.id.rb_suv: 
     iv_carType.setImageResource(R.drawable.suv); 
     sCar=rb_select.getText().toString(); 
     break; 
    default: 
     iv_carType.setImageResource(R.drawable.suv); 
     sCar="default"; 
} 
final String finalSCar = sCar; 
btn_confirmCar.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     tv_carType.setText(finalSCar); 
     dialog_car.dismiss(); 
    } 
}); 
dialog_car.show(); 
} 

ですデフォルトの条件だけを実行します。つまり、IDを取得しないことを意味します。事前のおかげで.. :)

+0

ここで/ whenは 'diplayCartype'が呼び出されていますか? – pskink

+0

displayCartypeは、テキストが表示されるテキストフィールドを含む線形レイアウトのクリックで呼び出されます – Adarsh

+0

なぜ 'RadioGroup#setOnCheckedChangeListener'を使用しませんか? – pskink

答えて

0

についてgetCheckedRadioButtonId()

このグループ内で選択されたラジオボタンの識別子を返します。 空の選択時には、戻り値は-1です。

選択したものを確認しようとしている瞬間に、RadioButtonが選択されていません。

dialog_car.setContentView(R.layout.dialog_cartype); 

// at this point you have newly created view hierarchy and 
// RadioGroup has no selected items 

iv_carType=(ImageView)dialog_car.findViewById(R.id.iv_carType); 
btn_confirmCar=(Button)dialog_car.findViewById(R.id.btn_confirmCar); 
rg_carType=(RadioGroup)dialog_car.findViewById(R.id.rg_carType); 

// still has no selected items, next call returns -1 as well 
int selected=rg_carType.getCheckedRadioButtonId(); 

ユーザーがあなたのラジオボタンを見て、何かを選択するのに十分な時間を持っていることを確認するために(例えば、「OK」)いくつかの余分なボタンのハンドラからgetCheckedRadioButtonId()を使用する必要があります。私にとって

+0

実行時にダイアログボックスが表示されていますが、radioButtonを選択していますが、スイッチケースがデフォルトになります – Adarsh

+0

@ Let'sLearnit選択されていない作成済みのビューから選択を取得しようとしています項目はまだありません。私の編集を参照してください。 – Sergio

+0

はい私はポイントを取得しています...選択されたアイテムがない場合は..私はデフォルトのチェック項目を置くことができます...しかし、それは単にデフォルトの項目を選択します...どのようにここから項目を選択することができます... – Adarsh

0

このコードの動作は

public class RadioButtonFragment extends DialogFragment { 
    String singleitem; 
    private int witch; 
    private MySharedPreferences mySharedPreferences = new MySharedPreferences(); 
    private String[] item = {"Naskh asiatype", "Fajer noori Nastaleeq", "Pak nastaleeq (default)"}; 

    @Override 
    public void onResume() { 
     super.onResume(); 
     witch= mySharedPreferences.loadIntPrefs(Constants.RADIO_BUTTON_INDEX_KEY,2,getActivity()); 

    } 


    @NonNull 
    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     witch= mySharedPreferences.loadIntPrefs(Constants.RADIO_BUTTON_INDEX_KEY,2,getActivity()); 
     builder.setTitle("please selet any fount").setSingleChoiceItems(item, witch, new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       singleitem= item[which]; 
       mySharedPreferences.saveStringPrefs(Constants.FONT_KEY,singleitem,getActivity()); 
       mySharedPreferences.saveintPrefs(Constants.RADIO_BUTTON_INDEX_KEY,which,getActivity()); 
       Toast.makeText(getContext(),"Font is selected"+which,Toast.LENGTH_SHORT).show(); 

      } 
     }); 

     return builder.create(); 
    } 
} 

とあなたが右の時間(ボタンクリック)で値を読み取る必要がある

RadioButtonFragment radioButtonFragment = new RadioButtonFragment(); 
     radioButtonFragment.show(getSupportFragmentManager(), RadioButtonFragment_TAG); 
0

このフラグメントを構築するために呼び出します。これを行う:あなたはユーザ入力を聴くためインタフェース(リスナー)を使用する必要があります

btn_confirmCar.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     rg_carType=(RadioGroup)dialog_car.findViewById(R.id.rg_carType); 

     int selected=rg_carType.getCheckedRadioButtonId(); 
     rb_select=(RadioButton)dialog_car.findViewById(selected); 

     switch(selected) 
     { 
      case R.id.rb_hatchD: 
       iv_carType.setImageResource(R.drawable.hatch_d); 
       break; 
      case R.id.rb_hatchP: 
       iv_carType.setImageResource(R.drawable.hatch_p); 
       break; 
      case R.id.rb_sedanD: 
       iv_carType.setImageResource(R.drawable.sedan_d); 
       break; 
      case R.id.rb_sedanP: 
       iv_carType.setImageResource(R.drawable.sedan_p); 
       break; 
      case R.id.rb_suv: 
       iv_carType.setImageResource(R.drawable.suv); 
       break; 
      default: 
       iv_carType.setImageResource(R.drawable.suv); 
       sCar="default"; 
     } 

     if (!sCar.equals("default")) 
      sCar = rb_select.getText().toString(); 

     tv_carType.setText(sCar); 
     dialog_car.dismiss(); 
    } 
}); 

dialog_car.show(); 
} 
0

。たとえば、RadioGroup.setOnCheckedChangeListenerを使用し、スイッチ文を入力します。

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 
      switch(checkedId){ 
        case R.id.radioButtonId: 
         // Your Code Here 
         break;      
        --- 
      } 
     } 
    }); 

現在のコードは、最初に割り当てられた状態のデフォルト値を静的に選択します。上記の拡張機能は、ユーザー入力を動的に処理するのに役立ちます。

関連する問題