短视频商城源码,安卓几种弹窗方法

2020-10-12 14:42:46 浏览数 (1)

代码语言:javascript复制
public class MainActivity extends AppCompatActivity {
    private Button btn, btn2, btn3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        OnClick onClick = new OnClick();
        btn = findViewById(R.id.button2);
        btn2 = findViewById(R.id.button3);
        btn3 = findViewById(R.id.button4);
        btn.setOnClickListener(onClick);
        btn2.setOnClickListener(onClick);
        btn3.setOnClickListener(onClick);
    }
    class OnClick implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                //单选框弹窗
                case R.id.button2:
                    final String s[] = new String[]{"男", "女"};
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setTitle("测试")
                            .setSingleChoiceItems(s, 0, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    Toast.makeText(MainActivity.this, s[i], Toast.LENGTH_LONG).show();
                                }
                            }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();//关闭弹窗,可写可不写,看情况
                        }
                    }).setCancelable(false)//不能点击弹窗之外的地方
                            .show();
                    break;
                    //多选弹窗
                case R.id.button3:
                    final String s1[] = new String[]{"时崎狂三", "乌贼娘", "momo"};
                    final boolean isok[] = new boolean[]{true, false, false};
                    AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this);
                    builder1.setTitle("你的老婆是").setMultiChoiceItems(s1, isok, new DialogInterface.OnMultiChoiceClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                            Toast.makeText(MainActivity.this, s1[i]   "选择状态"   isok[i], Toast.LENGTH_LONG).show();
                        }
                    }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                        }
                    }).show();
                    break;
                    //自定义弹窗
                case R.id.button4:
                    LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);//适配器
                    View v = layoutInflater.inflate(R.layout.dialoglayout, null);//引用自定义布局
                    Button button = findViewById(R.id.button4);//可以获取到自定义布局里面的控件
                    button.setOnClickListener(new View.OnClickListener() {//监听控件的事件
                        @Override
                        public void onClick(View view) {
                          //这里写事件代码
                        }
                    });
                    AlertDialog.Builder builder2 = new AlertDialog.Builder(MainActivity.this);//创建弹窗
                    builder2.setView(v).show();//设置自定义布局并show出来
                    break;
                 //透明弹窗
                case R.id.button_aploh:
                    Dialog dialog = new Dialog(MainActivity.this);
                    LayoutInflater layoutI = LayoutInflater.from(MainActivity.this);//适配器
                    View v2 = layoutI.inflate(R.layout.dialoglayout, null);//引用自定义布局
                    dialog.setContentView(v2);
                    dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);//最重要的一行
                    dialog.show();
                    break;
            }
        }
    }
}

0 人点赞