Android - AlertDialog 對話框
使用步驟:
| ① | 創建一個AlertDialog.Builder物件 |
|---|---|
| ② | 設定標題 內容 正面負面按鍵 撰寫點擊事件 |
| ③ | 呼叫.show() 顯示AlertDialog |
XML部分:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ph5"
android:layout_marginBottom="20dp"></ImageView>
<Button
android:id="@+id/alertDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AlertDialog"></Button>
</LinearLayout>

JAVA代碼:
public class MainActivity extends AppCompatActivity {
Button button;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.imageview);
button = findViewById(R.id.alertDialog);
imageView = findViewById(R.id.imageView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder alterDiaglog = new AlertDialog.Builder(MainActivity.this);
alterDiaglog.setTitle("提示");
alterDiaglog.setMessage("是否洗掉圖片?");
alterDiaglog.setPositiveButton("是", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
imageView.setImageResource(0);
}
});
//消極的選擇
alterDiaglog.setNegativeButton("否", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alterDiaglog.show();
}
});
}
}
點擊按鈕顯示對話框:

點擊是后圖片被洗掉

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/227566.html
標籤:其他
