哈囉大家好,想必在看過前幾篇的Dialog的基本用法後,會想要自己創造出一個客製化的Dialog,而今天要示範的Dialog可以讓使用者自己設計出心裡所想的Dialog,那我們就開始吧!
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btnCustomDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自定義Dialog"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
在activity_main裡面我只給他一個Button來觸發Dialog
新增一個layout來設計Dialog的畫面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical">
<LinearLayout
android:id="@+id/linearDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:gravity="center"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自訂義Dialog的內容"
android:textSize="25sp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#CAC6C6"
>
<Button
android:id="@+id/btnDialogConfirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="確認"
/>
<Button
android:id="@+id/btnDialogCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
在res -> values -> styles.xml裡面新增Dialog的格式
<style name="MDialog" parent="android:Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">false</item>
<item name="android:background">@android:color/background_light</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
接下來是MainActivity
package com.example.day30_dialog_4;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button btnCustomDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
btnCustomDialog = findViewById(R.id.btnCustomDialog);
btnCustomDialog.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btnCustomDialog:
final CustomDialog customDialog = new CustomDialog(MainActivity.this);
customDialog.setClicklistener(new CustomDialog.ClickListenerInterface() {
@Override
public void Confirm() {
customDialog.dismiss();
Toast.makeText(MainActivity.this,"您點選的是確認按鈕",Toast.LENGTH_SHORT).show();
}
@Override
public void Cancel() {
customDialog.dismiss();
}
});
customDialog.show();
break;
}
}
}
在MainActivity裡面我設了一個按鈕來觸發自定義的Dialog
###CustomDialog
package com.example.day30_dialog_4;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
public class CustomDialog extends Dialog {
private Context context;
private ClickListenerInterface clickListenerInterface;
public interface ClickListenerInterface {
void Confirm();
void Cancel();
}
public CustomDialog(Context context){
super(context,R.style.MDialog);
this.context = context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
public void init(){
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.dialog_layout,null);
setContentView(v);
Button btnDialogConfirm = findViewById(R.id.btnDialogConfirm);
Button btnDialogCancel = findViewById(R.id.btnDialogCancel);
btnDialogConfirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickListenerInterface.Confirm();
}
});
btnDialogCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickListenerInterface.Cancel();
}
});
Window dialogWindow = getWindow();
WindowManager.LayoutParams layoutParams = dialogWindow.getAttributes();
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
layoutParams.width = (int)(displayMetrics.widthPixels * 0.8);
dialogWindow.setAttributes(layoutParams);
}
public void setClicklistener(ClickListenerInterface clickListenerInterface) {
this.clickListenerInterface = clickListenerInterface;
}
}
在客製化的Dilaog裡面我示範了兩個按鈕,你們如果想要設置1個2個...幾個按鈕都可以,也可以設定要不要標題,那我們今天就示範到這了,謝謝大家的觀看。