您好,登錄后才能下訂單哦!
/**
* 自定義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);
}
/**
* 設(shè)置按鈕
*
*/
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);
}
/**
* 設(shè)置按鈕
*/
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);
}
}
/**
* 關(guān)閉對(duì)話框
*/
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>
以上是新建一個(gè)class文件,最關(guān)鍵的自定義代碼,以下就是在activity之中直接調(diào)用即可,中間用到的圖片什么的,自己選好添加即可,
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("標(biāo)題");
ad.setMessage("內(nèi)容sdfs容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容");
ad.setPositiveButton("確定", new OnClickListener() {
@Override
public void onClick(View v) {
ad.dismiss();
Toast.makeText(MainActivity.this, "被點(diǎn)到確定", Toast.LENGTH_LONG)
.show();
}
});
ad.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(View v) {
ad.dismiss();
Toast.makeText(MainActivity.this, "被點(diǎn)到取消", Toast.LENGTH_LONG)
.show();
}
});
}
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。