1.首先 新建立一个类 MessageBox 如下:
//=============================================
package zicox.sdk;
import android.app.Activity;
import android.app.Dialog;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
public class MessageBox extends Dialog {
int dialogResult;
Handler mHandler ;
public MessageBox(Activity context)
{
super(context);
dialogResult=0;
setOwnerActivity(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
onCreate();
}
public void onCreate()
{
setContentView(R.layout.messagebox);
findViewById(R.id.btnCancel).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View paramView)
{
endDialog(0);
}
});
findViewById(R.id.btnOK).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View paramView)
{
endDialog(1);
}
});
}
public int getDialogResult()
{
return dialogResult;
}
public void setDialogResult(int dialogResult)
{
this.dialogResult = dialogResult;
}
public void endDialog(int result)
{
dismiss();
setDialogResult(result);
Message m = mHandler.obtainMessage();
mHandler.sendMessage(m);
}
public int showDialog(String Msg,String Title)
{
TextView TvErrorInfo = (TextView) findViewById(R.id.textViewInfo);
TvErrorInfo.setText(Msg);
TextView TvTitle = (TextView) findViewById(R.id.textViewTitle);
TvTitle.setText(Title);
mHandler = new Handler() {
@Override
public void handleMessage(Message mesg) {
throw new RuntimeException();
}
};
super.show();
try
{
Looper.getMainLooper();
Looper.loop();
}
catch(RuntimeException e2)
{
}
return dialogResult;
}
}
2.调用的时候
MessageBox msgBox = new Messagebox(this);
msgBox.show(xx,xx);
这样就可以了
3.关于对话框样式 在 Messagebox.xml 中设置 我的设置如下;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:orientation="vertical" android:layout_width="255dp" android:background="#FFFFFF">
<TableRow android:layout_height="wrap_content" android:background="#444444" android:id="@+id/tableRow1" android:layout_width="fill_parent">
<TextView android:layout_height="wrap_content" android:textColor="#FFFFFF" android:background="#444444" android:textAppearance="?android:attr/textAppearanceLarge" android:text="TextView" android:textSize="24dp" android:layout_width="fill_parent" android:id="@+id/textViewTitle" android:layout_marginLeft="12dip" android:layout_marginRight="8dip" android:layout_marginTop="8dip" android:layout_marginBottom="8dip"></TextView>
</TableRow>
<TextView android:layout_height="wrap_content" android:textColor="#000000" android:gravity="center" android:textAppearance="?android:attr/textAppearanceLarge" android:text="TextView" android:id="@+id/textViewInfo" android:layout_width="fill_parent" android:singleLine="false" android:layout_marginLeft="4dip" android:layout_marginRight="4dip" android:layout_marginTop="12dip" android:layout_marginBottom="12dip"></TextView>
<LinearLayout android:background="#888888" android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="wrap_content">
<Button android:layout_height="wrap_content" android:text="确定" android:textSize="20dp" android:id="@+id/btnOK" android:layout_width="120dp" android:layout_marginLeft="4dip" android:layout_marginRight="3dip" android:layout_marginTop="8dip" android:layout_marginBottom="3dip"></Button>
<Button android:layout_height="wrap_content" android:text="取消" android:textSize="20dp" android:id="@+id/btnCancel" android:layout_width="120dp" android:layout_marginLeft="3dip" android:layout_marginRight="3dip" android:layout_marginTop="8dip" android:layout_marginBottom="3dip"></Button>
</LinearLayout>
</LinearLayout>