Android 自定义AlertDialog类
还是先从最简单的开始吧,然后一步一步的扩展。
为了保证软件上所谓的低耦合度和可重用性,这里不得不需要单独建立一个类CustomerDialog,然后继承AlertDialog
public class CustomerDialog extends AlertDialog {
}
然后添加一个带Context参数的构造器,context(上下文)通俗点讲一般是指归属于那个,这里就归属于调用的那个Acitivity,也就是说这个对话框是针对调用的那个Activity
public 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文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="显示自定义对话框" /> </RelativeLayout>
自定义dialog的xml文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/buttondialog" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="show AlertDialog" /> </LinearLayout>
主程序如下:
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基本使用详解:http://www.360doc.com/content/13/0917/23/9171956_315273612.shtml
http://blog.csdn.net/comkingfly/article/details/6578708

浙公网安备 33010602011771号