還是先從最簡單的開始吧,然後一步一步的擴充。
為了保證軟體上所謂的低耦合度和可重用性,這裡不得不需要單獨建立一個類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>
View Code
自定義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);
*/
}
}
顯示效果: