您好,登錄后才能下訂單哦!
/**
* 自定義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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。