今天对dialog做出了改进
package com.example.math.utils; import android.annotation.SuppressLint; import android.app.Dialog; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.Button; import androidx.annotation.NonNull; import com.example.math.AboutActivity; import com.example.math.AdvertActivity; import com.example.math.BingActivity; import com.example.math.BuActivity; import com.example.math.BuHisActivity; import com.example.math.R; import com.example.math.SettingActivity; public class MoreDialog extends Dialog implements View.OnClickListener { Button aboutBtn, bingingBtn, advertBtn, settingBtn, buBtn, buHisBtn; public MoreDialog(@NonNull Context context) { super(context); } @SuppressLint("MissingInflatedId") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dialog_more); aboutBtn = findViewById(R.id.dialog_btn_about); // bingingBtn = findViewById(R.id.dialog_btn_binging); advertBtn = findViewById(R.id.dialog_btn_advert); settingBtn = findViewById(R.id.dialog_btn_setting); buBtn = findViewById(R.id.dialog_btn_bu_huo); buHisBtn = findViewById(R.id.dialog_btn_bu); buHisBtn.setOnClickListener(this); aboutBtn.setOnClickListener(this); // bingingBtn.setOnClickListener(this); advertBtn.setOnClickListener(this); buBtn.setOnClickListener(this); settingBtn.setOnClickListener(this); } @SuppressLint("NonConstantResourceId") @Override public void onClick(View v) { Intent intent = new Intent(); switch (v.getId()) { case R.id.dialog_btn_about://关于我们 intent.setClass(getContext(), AboutActivity.class); getContext().startActivity(intent); break; /* case R.id.dialog_btn_binging://设备绑定 intent.setClass(getContext(), BingActivity.class); getContext().startActivity(intent); break;*/ case R.id.dialog_btn_advert://广告 intent.setClass(getContext(), AdvertActivity.class); getContext().startActivity(intent); break; case R.id.dialog_btn_bu_huo://补货记录 intent.setClass(getContext(), BuHisActivity.class); getContext().startActivity(intent); break; case R.id.dialog_btn_bu://补货 intent.setClass(getContext(), BuActivity.class); getContext().startActivity(intent); break; case R.id.dialog_btn_setting://设置 intent.setClass(getContext(), SettingActivity.class); getContext().startActivity(intent); break; } cancel(); } /* 设置Dialog的尺寸和屏幕尺寸一致*/ public void setDialogSize() { // 获取当前窗口对象 Window window = getWindow(); // 获取窗口对象的参数 WindowManager.LayoutParams wlp = window.getAttributes(); wlp.x = 1000; wlp.y = 120; wlp.gravity = Gravity.TOP; window.setBackgroundDrawableResource(android.R.color.transparent); window.setAttributes(wlp); } }
在界面中
case R.id.mel_btn_more: MoreDialog moreDialog = new MoreDialog(this); moreDialog.show(); moreDialog.setDialogSize(); break;
使用的界面为点击效果。
或者直接使用,不用适配器
private void showHuanDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("切换账号提示") .setMessage("请确认切换账号") .setPositiveButton("取消", null) .setNegativeButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { startActivity(new Intent(getApplicationContext(), LoginActivity.class)); finish(); Toast.makeText(SettingActivity.this, "下线成功", Toast.LENGTH_SHORT).show(); } }); builder.create().show(); }
标签:26,04,getContext,intent,dialog,2023,import,btn,id From: https://www.cnblogs.com/JJTyyds/p/17357433.html