我在一个包中创建了一个alertdialog类,我将其作为另一个类中的方法调用。我想要的是当用户点击alertdialog上的按钮时,它必须开始一项新活动。我不知道该怎么做请指导我。我是否需要在alertdialog类中创建一个方法,以便按钮执行操作,如果是这样的话。
Alertdialog.java
public class Alertdialog implements OnClickListener {
private static final int ALERT_DIALOG = 1;
int dialog;
Dialog dialog1;
Context dia;
String st;
Button bq;
String a;
public Alertdialog() {
}
@SuppressLint("NewApi")
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public Dialog customalertdialog(Context dia, int id, String r, String a,
String b, String b1) {
// protected Dialog onCreateDialog( ){
// this.dialog = ALERT_DIALOG;
this.dia = dia;
if (id == ALERT_DIALOG) {
AlertDialog.Builder builder = new AlertDialog.Builder(dia,
R.style.MyTheme);
AlertDialog alertDialog = builder.setPositiveButton(b, null)
.setNegativeButton(b1, null).setMessage(a).setTitle(r)
.create();
DisplayMetrics metrics = alertDialog.getContext().getResources()
.getDisplayMetrics();
final int width = metrics.widthPixels;
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
AlertDialog alertDialog = (AlertDialog) dialog;
View view = alertDialog.getWindow().getDecorView()
.findViewById(android.R.id.content);
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) view
.getLayoutParams();
layoutParams.width = 4 * width / 5; // 80% of screen
layoutParams.gravity = Gravity.CENTER;
view.setLayoutParams(layoutParams);
alertDialog.getWindow().setBackgroundDrawable(
new ColorDrawable(Color.TRANSPARENT));
}
});
alertDialog.show();
bq = alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
bq.setBackground(dia.getResources().getDrawable(
R.drawable.radibutton));
Button bq1 = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
bq1.setBackground(dia.getResources().getDrawable(
R.drawable.radibutton));
if (bq != null && bq1 != null) {
bq.setHeight(75);
bq1.setHeight(75);
}
}
return dialog1;
}
答案 0 :(得分:0)
您需要从您调用Alertdialog
类的活动中传递上下文,然后使用上下文来启动活动(意图)。
代码:...
public class Alertdialog implements OnClickListener {
private static final int ALERT_DIALOG = 1;
int dialog;
Dialog dialog1;
Context dia;
String st;
Button bq;
String a;
private Context context; //this line
public Alertdialog(Context context) {
this.context = context //this line
}
.....
然后在你的BUTTON_POSITIVE中添加:
Intent i = new Intent(context, whereverYouWantToGo.class);
context.startActivity(i);
希望你有这个想法..给我一个反馈是否有效。
答案 1 :(得分:0)
以下是调用活动的示例代码
Intent intent = new Intent(this, SelectCityActivity.class);
intent.putExtra(Statics.INTENT_PARAM_CITY_SEARCH_TITLE,"Choose a city");
intent.putExtra(Statics.INTENT_PARAM_CITY_SEARCH_SUB_TITLE,"where are you leaving from");
startActivityForResult(intent, PICK_FROM_CITY_REQUEST);
答案 2 :(得分:0)
请以简单的方式执行此操作。您将需要AlertDialogue,然后粘贴以下代码
AlertDialog alert_back = new AlertDialog.Builder(CurrentClassName.this).create();
alert_back.setTitle("Alert Dialog Title");
alert_back.setMessage("Alert Dialog Message");
alert_back.setButton2("Go", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent=new Intent(CurrentClassName.this,DestinationClassName.class);
startActivity(intent);
dialog.dismiss();
}
});
alert_back.show();
如果您想这样做,那就很容易了。
试试这个并提供反馈。