我试图让用户点击我的通话按钮然后打开一个警告对话框,其中包含2个选项(调用和取消按钮)我试图实现一些代码但抛出异常。我知道非常模糊,但要保持简单,我如何使用alerdialog实现我的目标
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if ((getArguments().getInt(ARG_SECTION_NUMBER)==1)) {
View view = inflater.inflate(R.layout.phones, container, false);
//button decloration
Button newPage = (Button)view.findViewById(R.id.view3);
newPage.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) { Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:07**********"));
startActivity(callIntent); }
});
return view;
}
答案 0 :(得分:0)
将以下代码放在一个方法中,并根据需要调用该方法。
AlertDialog alertDialog;
AlertDialog.Builder builder = new AlertDialog.Builder(BaseActivity.this);
alertDialog = builder.create();
alertDialog.setOnDismissListener(new myOnDismissListener());
alertDialog.setTitle("TITLE");
alertDialog.setMessage("Are you sure to call ?");
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//PUT YOUR CALL PHONE CODE HERE
}
});
alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE,"Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alertDialog.show();
}
class myOnDismissListener implements DialogInterface.OnDismissListener {
@Override
public void onDismiss(DialogInterface dialog) {
// TODO Auto-generated method stub
alertDialog.dismiss();
}
}