2017-05-11 57 views
-3

我想在RadioGroup中有一個帶3個RadioButtons的警報對話框,每個對話框都有一個圖像而不是單選按鈕的簡單視圖,並且基於此選擇進行更改我的主要活動。除非我無法將selected_user返回到mainActivity,否則一切正常。我搜查了很多,但我無法找到解決方案。我是Android新手,請幫助我。Android的 - 如何在AlertDialog中使用圖像而不是RadioButtons

mainActivity.java:

myImage.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       myAlert alert = new myAlert(this, value);//I use this value to check the item that was chosen in mainActivity before calling AlertDialog 
       alert.show(); 
      } 
     }); 

myAlert.java:

public class myAlert extends Dialog implements View.OnClickListener { 

Context context; 
String value; 
RadioButton rb1, rb2, rb3; 
RadioGroup rg; 

public myAlert(@NonNull Context context, String value) 
{ 
    super(context); 
    this.context = context; 
    this.value = value; 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.alert); 

    rb1 = (RadioButton)findViewById(R.id.rb1); 
    rb2 = (RadioButton)findViewById(R.id.rb2); 
    rb3 = (RadioButton)findViewById(R.id.rb3); 

    if(value.equals("car")) 
     rb1.setChecked(true); 
    else if(value.equals("bus")) 
     rb2.setChecked(true); 
    else 
     rb3.setChecked(true); 

    rb1.setOnClickListener(this); 
    rb2.setOnClickListener(this); 
    rb3.setOnClickListener(this); 
} 

@Override 
public void onClick(View v) 
{ 
    int selectId=rg.getCheckedRadioButtonId(); 
    RadioButton selected = (RadioButton)findViewById(selectId); 
    String selected_user = selected.getText().toString(); 

    dismiss(); 
}} 

alert.xml:

<RadioButton 
    android:id="@+id/rb1" 
    android:layout_width="100dp" 
    android:layout_height="100dp" 
    android:background="@drawable/draw" 
    android:button="@null" 
    android:drawableBottom="@drawable/ib_rb1_alert" 
    /> 

<RadioButton 
    android:id="@+id/rb3" 
    android:layout_width="100dp" 
    android:layout_height="100dp" 
    android:background="@drawable/draw" 
    android:button="@null" 
    android:drawableBottom="@drawable/ib_rb3_alert" /> 

<RadioButton 
    android:id="@+id/rb2" 
    android:layout_width="100dp" 
    android:layout_height="100dp" 
    android:background="@drawable/draw" 
    android:button="@null" 
    android:drawableBottom="@drawable/ib_rb2_alert" /> 

回答

0

如果我明白你的問題和代碼,那麼你不能回或者在主要活動上設置「selected_user」變量,如果是,則在主要活動上添加一個類別變量創建它的setter方法,並設置它的 「selected_user」 變量值,看看這個:

class MainActivity extends AppCompatActivity{ 
    String mySelectedUserID = ""; 

    public void setMySelectedUserID(String mySelectedUserID) { 
     this.mySelectedUserID = mySelectedUserID; 
    } 
} 

myAlert.class:

public class myAlert extends Dialog implements View.OnClickListener { 

       MainActivity context; 
       String value; 
       RadioButton rb1, rb2, rb3; 
       RadioGroup rg; 

       public myAlert(@NonNull MainActivity context, String value) { 
        super(context); 
        this.context = context; 
        this.value = value; 
       } 

       @Override 
       protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.alert); 

        rb1 = (RadioButton) findViewById(R.id.rb1); 
        rb2 = (RadioButton) findViewById(R.id.rb2); 
        rb3 = (RadioButton) findViewById(R.id.rb3); 

        if (value.equals("car")) 
         rb1.setChecked(true); 
        else if (value.equals("bus")) 
         rb2.setChecked(true); 
        else 
         rb3.setChecked(true); 

        rb1.setOnClickListener(this); 
        rb2.setOnClickListener(this); 
        rb3.setOnClickListener(this); 
       } 

       @Override 
       public void onClick(View v) { 
        int selectId = rg.getCheckedRadioButtonId(); 
        RadioButton selected = (RadioButton) findViewById(selectId); 
        String selected_user = selected.getText().toString(); 
        context.setMySelectedUserID(selected_user); 

        dismiss(); 
       } 
      } 
相關問題