AlertDialog基础使用

  1. 活动中定义方式一
public void warningInvliadCard(String invalidCard) {

new AlertDialog.Builder(this)

.setTitle(getString(R.string.invalidCard_head))

.setMessage(invalidCard)

.setPositiveButton(R.string.button_sure, new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

}

}).show();

}

public void warningUnknownHost() {

new AlertDialog.Builder(this)

.setTitle(activity.getString(R.string.UnknownHostException))

.setMessage(activity.getString(R.string.UnknownHostException_des))

.setPositiveButton(R.string.button_sure, new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

}

}).show();

}

public void warningTimeout() {

new AlertDialog.Builder(this)

.setTitle(activity.getString(R.string.SocketTimeoutException))

.setMessage(activity.getString(R.string.SocketTimeoutException_des))

.setPositiveButton(R.string.button_sure, new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

}

}).show();

}

public void warningConnectException() {

new AlertDialog.Builder(this)

.setTitle(activity.getString(R.string.ConnectException))

.setMessage(activity.getString(R.string.ConnectException_des))

.setPositiveButton(R.string.button_sure, new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

}

}).show();

}

public void warningClose() {

new AlertDialog.Builder(this)

.setTitle(activity.getString(R.string.closed))

.setMessage(activity.getString(R.string.closed_des))

.setPositiveButton(R.string.button_sure, new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

}

}).show();

}

public void warningUnknownError() {

new AlertDialog.Builder(this)

.setTitle(activity.getString(R.string.UnknownError))

.setMessage(activity.getString(R.string.UnknownError_eds))

.setPositiveButton(R.string.button_sure, new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

}

}).show();

}
  1. 活动中定义方式二
private AlertDialog alertDialog = null;

public void warningInvliadCard() {

if(alertDialog == null){

alertDialog = new AlertDialog.Builder(this,R.style.Theme_AppCompat_Light_Dialog_Alert)

.setTitle(getString(R.string.invalidCard_title))

.setMessage(R.string.invalidCard_Message)

.setPositiveButton(R.string.button_sure, new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

}

}).show();

}else{

if(alertDialog.isShowing()){

}else{

alertDialog.show();

}

}

}
  1. 活动中定义方式三(简单封装)
private AlertDialog alertDialog = null;

public void warningDialog(String title,String message) {

if(alertDialog == null){

alertDialog = new AlertDialog.Builder(this,R.style.Theme_AppCompat_Light_Dialog_Alert)

.setTitle(title)

.setMessage(message)

.setPositiveButton(R.string.button_sure, new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

}

}).show();

}else{

if(alertDialog.isShowing()){

}else{

alertDialog.show();

}

}

}

备注:

AlertDialog的最终封装使用见:「AlertDialog」(https://www.jianshu.com/p/0cd00cb01395)

你可能感兴趣的:(AlertDialog基础使用)