一、弹窗样式
在 themes.xml 布局中加入样式代码 Dialog_Msg
<style name="Dialog_Msg" parent="android:style/Theme.Dialog">
<!--背景颜色及和透明程度-->
<item name="android:windowBackground">@android:color/transparent</item>
<!--是否去除标题 -->
<item name="android:windowNoTitle">true</item>
<!--是否去除边框-->
<item name="android:windowFrame">@null</item>
<!--是否浮现在activity之上-->
<item name="android:windowIsFloating">true</item>
<!--是否模糊-->
<item name="android:backgroundDimEnabled">false</item>
</style>
二、弹窗布局
创建弹窗布局 mydialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="vertical">
<TextView
android:id="@+id/my_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="输入消息"
android:layout_gravity="center"
android:textSize="20sp"
/>
<EditText
android:id="@+id/my_text_edit"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#aaaaaa"
android:textSize="20sp" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/my_enter"
android:background="@color/lucency"
android:layout_width="58dp"
android:layout_height="50dp"
android:gravity="center"
android:layout_gravity="right"
android:text="确定"
android:textSize="20sp"/>
<Button
android:id="@+id/my_cancel"
android:background="@color/lucency"
android:layout_width="58dp"
android:layout_height="50dp"
android:gravity="center"
android:layout_gravity="left"
android:text="取消"
android:textSize="20sp"/>
</FrameLayout>
</LinearLayout>
三、弹窗自定义类 MyDialog
package com.example.myapplication03.dialog;
import android.app.Dialog;
import android.content.Context;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.NonNull;
import com.example.myapplication03.R;
public class MyDialog extends Dialog {
private Button my_enter,my_cancel;
private TextView my_title;
private EditText my_msg;
private String editText;
private String title;
private onEnterClickListener onEnterClickListener;
private onCancelClickListener onCancelClickListener;
public MyDialog(@NonNull Context context) {
super(context, R.style.Dialog_Msg);
}
public void setOnEnterClickListener(MyDialog.onEnterClickListener onEnterClickListener) {
this.onEnterClickListener = onEnterClickListener;
}
public void setOnCancelClickListener(onCancelClickListener onCancelClickListener){
this.onCancelClickListener = onCancelClickListener;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mydialog);
setCanceledOnTouchOutside(false);
initDate();
initView();
initEvent();
}
public void initView(){
my_enter = findViewById(R.id.my_enter);
my_cancel = findViewById(R.id.my_cancel);
my_title = findViewById(R.id.edit_title);
my_msg = findViewById(R.id.my_text_edit);
}
public void initEvent(){
my_enter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onEnterClickListener != null){
onEnterClickListener.onEnter(my_msg.getText().toString());
}
}
});
my_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onCancelClickListener != null){
onCancelClickListener.onCancel();
}
}
});
}
public void initDate(){
if (title != null){
my_title.setText(title);
}
if (editText != null){
my_msg.setText(editText);
}
}
public interface onEnterClickListener{
public void onEnter(String text);
}
public interface onCancelClickListener{
public void onCancel();
}
@Override
public void show() {
super.show();
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.width= ViewGroup.LayoutParams.MATCH_PARENT;
layoutParams.height= ViewGroup.LayoutParams.MATCH_PARENT;
getWindow().getDecorView().setPadding(0, 0, 0, 0);
getWindow().setAttributes(layoutParams);
}
}
四、使用 创建Activity
在 activity 中通过点击按钮触发
package com.example.myapplication03;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.os.strictmode.ImplicitDirectBootViolation;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import com.example.myapplication03.dialog.EditDialogTest;
import com.example.myapplication03.dialog.MyDialog;
import com.example.myapplication03.util.ToastUtil;
public class MainActivity6 extends AppCompatActivity implements View.OnClickListener {
private Context mContext = this;
View mview = this.getCurrentFocus();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main6);
findViewById(R.id.btn_my_dialog).setOnClickListener(this);
}
@Override
public void onClick(View v) {
final MyDialog myDialog = new MyDialog(this);
myDialog.setOnEnterClickListener(new MyDialog.onEnterClickListener() {
@Override
public void onEnter(String text) {
ToastUtil.show(mContext,"msg = "+text);
InputMethodManager imm =(InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getApplicationWindowToken(),0);
myDialog.dismiss();
}
});
myDialog.setOnCancelClickListener(new MyDialog.onCancelClickListener() {
@Override
public void onCancel() {
myDialog.dismiss();
}
});
myDialog.show();
}
}
activity 对应的 布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity6"
android:orientation="vertical">
<Button
android:id="@+id/btn_my_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="自定义弹窗" />
</LinearLayout>