/**
成都网站建设哪家好,找创新互联!专注于网页设计、成都网站建设、微信开发、微信小程序、集团成都企业网站定制等服务项目。核心团队均拥有互联网行业多年经验,服务众多知名企业客户;涵盖的客户类型包括:小搅拌车等众多领域,积累了大量丰富的经验,同时也获得了客户的一致称誉!
* 自定义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:::
android:layout_width="300sp" android:layout_height="match_parent" android:orientation="vertical" > android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/ss1" android:orientation="vertical" > android:id="@+id/title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:textColor="#000000" android:textSize="35dp" /> 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" /> 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" >
以上是新建一个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();
}
});
}
}