/**
* 自定义AlertDialog
* 用于退出程序按钮
* */
public class AlertDialog {
Context context;
android.app.AlertDialog ad;
TextView titleView;
TextView messageView;
LinearLayout buttonLayout;
public AlertDialog(Context context) {
this.context = context;
ad = new android.app.AlertDialog.Builder(context).create();
ad.show();
Window window = ad.getWindow();
window.setContentView(R.layout.alertdialog);
titleView = (TextView) window.findViewById(R.id.title);
messageView = (TextView) window.findViewById(R.id.message);
buttonLayout = (LinearLayout) window.findViewById(R.id.buttonLayout);
}
public void setTitle(int resId) {
titleView.setText(resId);
}
public void setTitle(String title) {
titleView.setText(title);
}
public void setMessage(int resId) {
messageView.setText(resId);
}
public void setMessage(String message) {
messageView.setText(message);
}
/**
* 设置按钮
*
*/
public void setPositiveButton(String text,
final View.OnClickListener listener) {
Button button = new Button(context);
LinearLayout.LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setBackgroundResource(R.drawable.alertdialog_button);
button.setText(text);
button.setTextColor(Color.WHITE);
button.setTextSize(20);
button.setOnClickListener(listener);
buttonLayout.addView(button);
}
/**
* 设置按钮
*/
public void setNegativeButton(String text,
final View.OnClickListener listener) {
Button button = new Button(context);
LinearLayout.LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setBackgroundResource(R.drawable.alertdialog_button);
button.setText(text);
button.setTextColor(Color.WHITE);
button.setTextSize(20);
button.setOnClickListener(listener);
if (buttonLayout.getChildCount() > 0) {
params.setMargins(40, 0, 0, 0);
button.setLayoutParams(params);
buttonLayout.addView(button, 1);
} else {
button.setLayoutParams(params);
buttonLayout.addView(button);
}
}
/**
* 关闭对话框
*/
public void dismiss() {
ad.dismiss();
}
}
alertDialog.xml:::
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300sp"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ss1"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textColor="#000000"
android:textSize="35dp" />
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:textColor="#000000"
android:textSize="25dp" />
<LinearLayout
android:id="@+id/buttonLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="5sp"
android:gravity="center"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
</LinearLayout>
以上是新建一个class文件,最关键的自定义代码,以下就是在activity之中直接调用即可,中间用到的图片什么的,自己选好添加即可,
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final AlertDialog ad = new AlertDialog(MainActivity.this);
ad.setTitle("标题");
ad.setMessage("内容sdfs容内容内容内容内容内容内容");
ad.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(View v) {
ad.dismiss();
Toast.makeText(MainActivity.this, "被点到确定", Toast.LENGTH_LONG)
.show();
}
});
ad.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(View v) {
ad.dismiss();
Toast.makeText(MainActivity.this, "被点到取消", Toast.LENGTH_LONG)
.show();
}
});
}
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。