1

私のsign_up.xmlファイルは次のとおりです。

public class sign_upActivity extends Activity implements AlertPositiveListener{

private RadioGroup radioGenderGroup,radioEduGroup;
private RadioButton radioGenderButton,radioEduButton;
int position = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sign_up);
<LinearLayout
        android:id="@+id/linearLayout8"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <Button
            android:id="@+id/gender_btn"
            android:layout_width="30dp"
            android:layout_height="34dp"
            android:layout_weight="0.06"
            android:background="@drawable/arraw_left" />

        <TextView
            android:id="@+id/gender_tv"
            android:layout_width="wrap_content"
            android:textColor="@color/Black"
            android:gravity="right"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout10"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <Button
             android:id="@+id/education_btn"
             android:layout_width="30dp"
             android:layout_height="34dp"
             android:layout_weight="0.06"
             android:background="@drawable/arraw_left" />

        <TextView
            android:id="@+id/education_tv"
            android:layout_width="wrap_content"
            android:gravity="right"
            android:textColor="@color/Black"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="" />

    </LinearLayout>

私は2つボタンを持っています。それらのいずれかをクリックすると、ポップアップ メニューが表示され、ユーザーにラジオ ボタンの選択を求め、[OK] をクリックします。他のボタンをクリックすると、別のポップアップ メニューが表示され、別のボタンが表示されます。私のアクティビティは次のとおりです。

 OnClickListener listener = new OnClickListener() {         
        @Override
        public void onClick(View v) {

            FragmentManager manager = getFragmentManager();
            AlertDialogRadio alert = new AlertDialogRadio();
            Bundle b  = new Bundle();
            b.putInt("position", position);
            alert.setArguments(b);
            alert.show(manager, "alert_dialog_radio");

        }
    }; 
    Button btn = (Button) findViewById(R.id.gender_btn);
    btn.setOnClickListener(listener);

}

@Override
public void onPositiveClick(int position) {

        this.position = position;
        TextView tv = (TextView) findViewById(R.id.gender_tv);
        tv.setText(Gender.code[this.position]); 
}

}

AlertDialogRadio は次のとおりです。

public class AlertDialogRadio  extends DialogFragment{


/** Declaring the interface, to invoke a callback function in the implementing activity class */
AlertPositiveListener alertPositiveListener;


/** An interface to be implemented in the hosting activity for "OK" button click listener */
interface AlertPositiveListener {
    public void onPositiveClick(int position);

    void onPositiveClick(int position, int type);
}


/** This is a callback method executed when this fragment is attached to an activity. 
 *  This function ensures that, the hosting activity implements the interface AlertPositiveListener 
 * */
public void onAttach(android.app.Activity activity) {
    super.onAttach(activity);
    try{
        alertPositiveListener = (AlertPositiveListener) activity;
    }catch(ClassCastException e){
        // The hosting activity does not implemented the interface AlertPositiveListener
        throw new ClassCastException(activity.toString() + " must implement AlertPositiveListener");
    }
}


/** This is the OK button listener for the alert dialog, 
 *  which in turn invokes the method onPositiveClick(position) 
 *  of the hosting activity which is supposed to implement it
 */ 
OnClickListener positiveListener = new OnClickListener() {      
    @Override
    public void onClick(DialogInterface dialog, int which) {
        AlertDialog alert = (AlertDialog)dialog;
        int position = alert.getListView().getCheckedItemPosition();            
        alertPositiveListener.onPositiveClick(position);            
    }
};

/** This is a callback method which will be executed 
 *  on creating this fragment
 */
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    /** Getting the arguments passed to this fragment */
    Bundle bundle = getArguments();     
    int position = bundle.getInt("position");


    /** Creating a builder for the alert dialog window */
    AlertDialog.Builder b = new AlertDialog.Builder(getActivity());


    /** Setting a title for the window */
    b.setTitle("Choose your version");      


    /** Setting items to the alert dialog */
    b.setSingleChoiceItems(Gender.code, position, null);


    /** Setting a positive button and its listener */
    b.setPositiveButton("OK",positiveListener);

    /** Setting a positive button and its listener */
    b.setNegativeButton("Cancel", null);


    /** Creating the alert dialog window using the builder class */
    AlertDialog d = b.create();

    /** Return the alert dialog window */
    return d;
}

}

しかし、ポップアップ メニューの 1 つしか実装できません。誰も私を助けますか?

4

0 に答える 0