适用性强的弹窗
Posted “我们的歌”
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了适用性强的弹窗相关的知识,希望对你有一定的参考价值。
new 一个Java类 PermissionDialog extends Dialog
java 代码
public class PermissionDialog extends Dialog
private Context mContext;
private TextView tv_title;
private TextView tv_message;
private Button btnLeft;
private Button btnRight;
private OnClickCallbackListener onClickCallbackListener;
public PermissionDialog(@NonNull Context context, int themeResId)
super(context, themeResId);
mContext = context;
public void setOnClickCallbackListener(OnClickCallbackListener listener)
onClickCallbackListener = listener;
@Override
protected void onCreate(Bundle savedInstanceState)
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_permission);
//获取屏幕宽高
WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int width = display.getWidth();
//设置弹窗大小
Window dialogWindow = getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
dialogWindow.setGravity(Gravity.CENTER);
//将对话框的大小按屏幕大小的百分比设置
// lp.width = (int)(width*0.8); // 宽度
lp.width = width; // 宽度
dialogWindow.setAttributes(lp);
//点击外面不消失
setCancelable(false);
tv_title = findViewById(R.id.tv_title);
tv_message = findViewById(R.id.tv_message);
tv_title.setText( R.string.permission_apply );
tv_message.setText( R.string.permission_content );
btnLeft = findViewById(R.id.btn_left);
btnRight = findViewById(R.id.btn_rigth);
btnLeft.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
if (onClickCallbackListener != null)
onClickCallbackListener.onClickLeftBtn();
dismiss();
);
btnRight.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
if (onClickCallbackListener != null)
onClickCallbackListener.onClickRightBtn();
dismiss();
);
public interface OnClickCallbackListener
void onClickLeftBtn(); //取消按钮
void onClickRightBtn(); //确定按钮
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:background="@drawable/bg_white_rectangle_6dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin_size_24dp"
android:orientation="vertical">
<TextView
android:id="@+id/tv_title"
style="@style/TextViewSize16Black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/margin_size_16dp"
android:textStyle="bold"
android:text="标题名称"/>
<TextView
android:id="@+id/tv_message"
style="@style/TextViewSize14Black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lineSpacingExtra="@dimen/margin_size_6dp"
android:text="标题名称标题"/>
<RelativeLayout
android:id="@+id/rl_button_layout_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_size_30dp"
android:visibility="visible">
<Button
style="@style/TextViewSize14MainColor"
android:id="@+id/btn_left"
android:layout_width="@dimen/view_size_130dp"
android:layout_height="@dimen/view_size_40dp"
android:textSize="@dimen/text_size_14sp"
android:textAllCaps="false"
android:text="@string/no"
android:background="@drawable/bg_white_round_corner_6dp20"
android:layout_alignParentLeft="true"/>
<Button
style="@style/TextViewSize14White"
android:id="@+id/btn_rigth"
android:layout_width="@dimen/view_size_130dp"
android:layout_height="@dimen/view_size_40dp"
android:background="@drawable/bg_round_corner_6dp"
android:textSize="@dimen/text_size_14sp"
android:textAllCaps="false"
android:text="@string/yes"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
哪里用代码考过去就行
final PermissionDialog dialog = new PermissionDialog( this, R.style.Theme_AppCompat_Dialog );
dialog.setOnClickCallbackListener( new PermissionDialog.OnClickCallbackListener( )
@Override
public void onClickLeftBtn()
PreferencesUtils.saveLong( AppConstants.SP_SAVE_FILE_NAME, "onetime", LoginActivity.onetime );
dialog.cancel( );
@Override
public void onClickRightBtn()
PreferencesUtils.saveLong( AppConstants.SP_SAVE_FILE_NAME, "onetime", LoginActivity.onetime );
doPermission( );
);
dialog.show( );
以上是关于适用性强的弹窗的主要内容,如果未能解决你的问题,请参考以下文章