标签:
1 package com.example.myandroid01; 2 3 import android.support.v7.app.ActionBarActivity; 4 import android.os.Bundle; 5 import android.view.Menu; 6 import android.view.MenuItem; 7 import android.view.View; 8 import android.widget.Button; 9 import android.widget.Toast; 10 /** 11 * Android 封装Dialog 12 * @Describe: 13 * @package: com.example.myandroid01 14 * @author shaobn 15 * @date 2015-9-14 下午2:35:49 16 */ 17 public class MainActivity extends ActionBarActivity { 18 private Button button1; 19 private Button button2; 20 private CustomDialog cdCustomDialog; 21 private static final String TITLE = "Warning"; 22 private static final String MESSAGE = "确定删除?"; 23 private static final String BUTTONNAME = "确定"; 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.activity_main); 28 button1 = (Button) this.findViewById(R.id.button1); 29 button2 = (Button) this.findViewById(R.id.button2); 30 button1.setOnClickListener(new View.OnClickListener() { 31 32 @Override 33 public void onClick(View arg0) { 34 // TODO Auto-generated method stub 35 cdCustomDialog = new CustomDialog(MainActivity.this); 36 cdCustomDialog.createDialog(TITLE,MESSAGE, BUTTONNAME,new DialogInter() { 37 38 @Override 39 public void callback(boolean flag) { 40 // TODO Auto-generated method stub 41 Toast.makeText(getApplicationContext(), "已点击", 1).show(); 42 } 43 }); 44 } 45 }); 46 button2.setOnClickListener(new View.OnClickListener() { 47 48 @Override 49 public void onClick(View arg0) { 50 // TODO Auto-generated method stub 51 cdCustomDialog = new CustomDialog(MainActivity.this); 52 cdCustomDialog.toastDialog("sure"); 53 } 54 }); 55 } 56 }
1 package com.example.myandroid01; 2 3 import android.app.AlertDialog; 4 import android.content.Context; 5 import android.content.DialogInterface; 6 import android.content.DialogInterface.OnClickListener; 7 import android.widget.Toast; 8 /** 9 * 自定义dialog类 10 * @Describe: 11 * @package: com.example.myandroid01 12 * @author shaobn 13 * @date 2015-9-14 下午2:36:17 14 */ 15 class CustomDialog { 16 private Context context; 17 private AlertDialog.Builder builder; 18 public CustomDialog(Context context) { 19 // TODO Auto-generated constructor stub 20 this.context = context; 21 } 22 public void createDialog(String title,String message,String buttonName,final DialogInter dialogInter){ 23 builder = new AlertDialog.Builder(context); 24 builder.setTitle(title); 25 builder.setMessage(message); 26 builder.setPositiveButton(buttonName, new OnClickListener() { 27 28 @Override 29 public void onClick(DialogInterface arg0, int arg1) { 30 // TODO Auto-generated method stub 31 dialogInter.callback(true); 32 } 33 }); 34 builder.create().show(); 35 } 36 public void toastDialog(String message){ 37 Toast.makeText(context, message,Toast.LENGTH_LONG).show(); 38 } 39 40 } 41 interface DialogInter{ 42 void callback(boolean flag); 43 }
标签:
原文地址:http://www.cnblogs.com/assassin666/p/4807102.html