gpt4 book ai didi

android - 如何在单独的类中编写一个函数来显示警报框

转载 作者:太空狗 更新时间:2023-10-29 16:38:00 25 4
gpt4 key购买 nike

package test;
import test.DisplayAlert;


public class AlertCall
{ int j;

public int dis2(String phoneNumber)
{
DisplayAlert ob = new DisplayAlert();
j=ob.dis1(AlertCall.this, phoneNumber);
return j;
}
}

显示对话框的类

package test;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;

public class DisplayAlert extends Activity implements OnClickListener
{
int t;

public int dis(Context activityContext, String destinationAddress)
{
new AlertDialog.Builder(activityContext).setTitle("SEND MESSAGE")
.setMessage("Are you sure you want to send this msg to no ? "+ destinationAddress)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which)
{

t=0;
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {

t=1;
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
return t;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

}
public int dis1(AlertCall alertCall, String phoneNumber) {
// TODO Auto-generated method stub
new AlertDialog.Builder(alertCall).setTitle("SEND MESSAGE")
.setMessage("Are you sure you want to send this msg to no ? "+ phoneNumber)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which)
{

t=0;
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {

t=1;
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
return t;
}


}

我编写了一个类来显示警报框并通过单独的调用调用它但它不起作用。当我从 main_Activity 调用该函数时它正在工作但是如果我从另一个类调用然后出现错误任何人都可以请建议。

最佳答案

使用回调

public class AlertDialogHelper {

public static void showAlert(Context context, final Callback callback) {
new AlertDialog.Builder(context).setTitle("Some Title").setMessage("Some message")
.setPositiveButton("Positive button", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
callback.onSucess(0);
}
}).setNegativeButton("Negative button", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
callback.onSucess(-1);
}
}).show();
}

public interface Callback {

public void onSucess(int t);

}

}


public class AlerCall extends Activity {

private int j;

public void dis() {
AlertDialogHelper.showAlert(this, new AlertDialogHelper.Callback() {

@Override
public void onSucess(int t) {
j = t;
}
});
}

}

关于android - 如何在单独的类中编写一个函数来显示警报框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23533313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com