还是先从最简单的开始吧,然后一步一步的扩展。
为了保证软件上所谓的低耦合度和可重用性,这里不得不需要单独建立一个类CustomerDialog,然后继承AlertDialogpublic class CustomerDialog extends AlertDialog { } 然后添加一个带Context参数的构造器,context(上下文)通俗点讲一般是指归属于那个,这里就归属于调用的那个Acitivity,也就是说这个对话框是针对调用的那个Activitypublic CustomerDialog(Context context) { super(context); this.context = context; } 接下来需要对AlertDialog的 onCreate方法覆盖,否则在外面就无法获得你创建的那个自定义对话框的内容了(当然你也可以直接在构造方法里调用setView,当这样一来耦合度就增加了),然后把自己的自定义内容通过setView关联进去。@Override protected void onCreate(Bundle savedInstanceState) { TextView textView = new TextView(context); textView.setText("这是一个自定义对话框"); textView.setTextSize(24); textView.setTextColor(Color.BLACK); setView(textView); super.onCreate(savedInstanceState); } 具体实现:主xml文件:自定义dialog的xml文件:
主程序如下:
package com.example.androidalertdialogcustometest;import android.os.Bundle;import android.app.Activity;import android.app.Dialog;import android.view.Menu;import android.view.View;import android.widget.Button;public class MainActivity extends Activity { private static final int CUSTOMER_DIALOG=1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) findViewById(R.id.button); View.OnClickListener listener = new View.OnClickListener() { @SuppressWarnings("deprecation") @Override public void onClick(View view) { showDialog(CUSTOMER_DIALOG); } }; button.setOnClickListener(listener); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override protected Dialog onCreateDialog(int id) { // TODO Auto-generated method stub CustomerDialog dialog = null; switch(id) { case CUSTOMER_DIALOG: dialog = new CustomerDialog(MainActivity.this); dialog.setTitle("自定义对话框"); dialog.setIcon(R.drawable.ia); break; } return dialog; }}
自定义AlertDialog类:
package com.example.androidalertdialogcustometest;import android.app.AlertDialog;import android.content.Context;import android.content.DialogInterface;import android.graphics.Color;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;public class CustomerDialog extends AlertDialog { private Context mContext; protected CustomerDialog(Context context) { super(context); // TODO Auto-generated constructor stub this.mContext=context; } @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(R.layout.dialog); Button button=(Button)findViewById(R.id.buttondialog); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(mContext, "dialog light", Toast.LENGTH_SHORT).show(); } }); /* TextView textView = new TextView(mContext); textView.setText("这是一个自定义对话框"); textView.setTextSize(24); textView.setTextColor(Color.BLACK); setView(textView); */ }}
显示效果:
AlertDialog基本使用详解: