我的 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>
我有两个按钮。当单击其中一个时,会出现一个弹出菜单并要求用户选择单选按钮,然后单击确定。当单击另一个按钮时,会出现另一个弹出菜单并有另一个按钮。我的活动是:
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;
}
}
但我只能实现其中一个弹出菜单。有人帮我吗?