Dialog写法(内含sp,seekbar相关)

1.新建一个布局xml文件

<?xml version="1.0" encoding="utf-8"?>

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:elevation="0dp"
    app:cardBackgroundColor="#777777"
    app:cardCornerRadius="20dp">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        android:paddingStart="30dp"
        android:paddingEnd="30dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="20dp"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/left"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:background="@drawable/left" />


            <androidx.cardview.widget.CardView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:layout_marginEnd="10dp"
                app:cardBackgroundColor="@color/white"
                app:cardCornerRadius="10dp">

                <TextView
                    android:id="@+id/speed"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="20dp"
                    android:layout_marginEnd="20dp"
                    android:text="1.0"
                    android:textColor="@color/black"
                    android:textSize="30sp"
                    android:textStyle="bold" />
            </androidx.cardview.widget.CardView>


            <ImageView
                android:id="@+id/right"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:background="@drawable/right" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="20dp"
            android:layout_weight="1"
            android:gravity="center">

            <SeekBar
                android:id="@+id/seekbar"
                style="@style/Widget.AppCompat.ProgressBar.Horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:min="5"
                android:progress="50" />

        </LinearLayout>


    </LinearLayout>


</androidx.cardview.widget.CardView>

2.然后写一个类继承它

public class CustomDialog extends Dialog {
    TextView tvSpeed;
    SeekBar seekBar;
    public float speedNow;
    

    public CustomDialog(@NonNull Context context) {
        super(context);
    }

    public CustomDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected CustomDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_custom_dialog);
        setCancelable(true);
        tvSpeed = findViewById(R.id.speed);
        seekBar = findViewById(R.id.seekbar);
        seekBar.setThumb(new ColorDrawable(Color.TRANSPARENT));
        tvSpeed.setText("" + format(speedNow));
        seekBar.setProgress((int) (speedNow * 50));

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                CustomDialog.this.speedNow = progress;
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    public void setSpeedNow(float speed) {
        this.speedNow = speed;
    }

    
    @Override
    protected void onStop() {
        super.onStop();
        SharedPreferences sp = getContext().getSharedPreferences("spconfig", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putFloat("speed", speedNow).apply();
        edit.commit();
    }

}

3.最后在activity中使用即可

customDialog = new CustomDialog(this);
                SharedPreferences sp = PlayActivity.this.getSharedPreferences("spconfig", Context.MODE_PRIVATE);
                customDialog.setSpeedNow(sp.getFloat("speed", 1));
                customDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
                customDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        SharedPreferences sp = PlayActivity.this.getSharedPreferences("spconfig", Context.MODE_PRIVATE);
                    }
                });
customDialog.show();

注意,要把上面框去掉

<style name="MyDialogStyle" parent="Base.Theme.AppCompat.Light.Dialog">
    <item name="android:windowNoTitle">true</item>
</style>
customDialog = new CustomDialog(this,R.style.MyDialogStyle);

为了点框外立即能消失

@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {
    Window window = getWindow();
    if (null == window) {
        return false;
    }

    final View decorView = window.getDecorView();
    if (isShowing() && shouldCloseOnTtouch(event, decorView)) {
        cancel();
        return true;
    }
    return false;
}

private boolean shouldCloseOnTtouch(MotionEvent event, View decorView) {
    final int x = (int) event.getX();
    final int y = (int) event.getY();
    final int slop = ViewConfiguration.get(decorView.getContext()).getScaledWindowTouchSlop();
    return x <= 0 || y <= 0 || x > decorView.getWidth() || y > decorView.getHeight();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值