今天學習系統的提醒對話框
利用系統對話框來選擇顯示甚麼訊息
public class AlertDialogActivity extends AppCompatActivity implements View.OnClickListener {
private TextView tv_alert;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_alert_dialog);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
findViewById(R.id.btn_alert).setOnClickListener(this);
tv_alert = findViewById(R.id.tv_alert);
}
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("親愛的用戶");
builder.setMessage("你真的要卸載嗎?");
builder.setPositiveButton("確定卸載", (dialogInterface, i) -> {
tv_alert.setText("謝謝您的配伴,後會有期");
});
builder.setNegativeButton("取消卸載", (dialogInterface, i) -> {
tv_alert.setText("我會持續提升自己的");
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}