Android中Activity之间的跳转 ProgressBar进度条 PopupWindow 弹出框 Dialog 对话框 Spinner 下拉框

目录

1.Activity之间的跳转

2.ProgressBar 进度条

3.PopupWindow 弹出框 

4.Dialog 对话框

5.Spinner 下拉框


1.Activity之间的跳转

        1.先new一个Intent设置意图

        2.括号里面是从哪里跳转到哪里

        3.startActivity是开始跳转

Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startACtivity(intent);

2.ProgressBar 进度条

 ProgressBar是Android中原生的进度条,分为环行进度条和圆形进度条,由style控制

<?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"
    android:orientation="vertical"
    android:padding="30dp"
    tools:context=".MainActivity2">
 
    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
 
    <ProgressBar
        android:layout_marginTop="30dp"
        android:id="@+id/progressBar2"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
LinearLayout/>

3.PopupWindow 弹出框 

            1.一种是在main方法里定义

private Button but_aaa;
TextView textView=new TextView(MainActivity3.this);
textView.setText("谈话框出现了!!!");

PopupWindow popupWindow=new PopupWindow(textView,300,400);
popupWindow.setOutsideTouchable(true);
but_aaa=findViewById(R.id.but_aaa);

        2.一种是先在layout里定义一个xml文件之后引用

// 创建一个另外弹窗
// 将xml文件转化成view
View pop_item = getLayoutInflater().inflate(R.layout.pop,null);
PopupWindow popupWindow1=new PopupWindow(pop_item,400,400);
//从外部取消
popupWindow1.setOutsideTouchable(true);
//设置背景图
pop_item.setBackground(getResources().getDrawable(R.drawable.ic_launcher_background));
//设置动画效果
pop_item.setElevation(1000f);

but_aaa.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        popupWindow1.showAsDropDown(but_aaa,but_aaa.getWidth(),but_aaa.getHeight());
    }
});

4.Dialog 对话框

1.简单对话框

but_1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //设置对话框
            AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity4.this)
                    .setTitle("警告")
                    .setMessage("确定删除该条数据吗?")
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Toast.makeText(MainActivity4.this,"取消",Toast.LENGTH_SHORT).show();
                        }
                    })
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Toast.makeText(MainActivity4.this,"确定",Toast.LENGTH_SHORT).show();
                        }
                    });
            builder.show();
        }
    });
}

2.列表对话框

private View.OnClickListener but_2a =new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        String[] strings={"A照","B照","C照","D照"};
        AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity4.this)
                .setTitle("请选择驾照类型:")
                .setItems(strings, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        mad = strings[i];
                        Toast.makeText(MainActivity4.this,"你的驾照类别为:"+mad,Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });
        builder.show();
    }
};

3.单选列表对话框

private View.OnClickListener but_3a = new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        String[] strings={"A照","B照","C照","D照"};
        AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity4.this)
                .setTitle("请选择驾照类型:")
                .setSingleChoiceItems(strings, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        mad=strings[i];

                    }
                })
                .setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        Toast.makeText(MainActivity4.this,"你的驾照类别为:"+mad,Toast.LENGTH_SHORT).show();
                    }
                })
                .setPositiveButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });
        builder.show();

    }
};

4.单多选列表对话框

private View.OnClickListener but_4a=new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        String[] strings={"A照","B照","C照","D照"};
        boolean[] booleans={true,false,true,false};
        AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity4.this)
                .setTitle("请选择你的驾照类别:")
                .setMultiChoiceItems(strings, booleans, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                        booleans[i]=b;
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        mad="";
                        for (int j=0;j<booleans.length;j++){
                            if (booleans[j]){
                                mad=mad+strings[j]+",";
                            }
                        }
                        Toast.makeText(MainActivity4.this,"你的驾照类别为:"+mad,Toast.LENGTH_SHORT).show();
                    }
                });

        builder.show();
    }
};

5.自定义列表对话框

private View.OnClickListener but_5a=new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            View view1=getLayoutInflater().inflate(R.layout.hua,null);
            AlertDialog.Builder builde=new AlertDialog.Builder(MainActivity4.this)
                    .setTitle("请输入你的账号密码:")
                    .setView(view1)
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                        }
                    })
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            //取出用户的账号和密码
                            EditText et_user=view1.findViewById(R.id.et_1);
                            String user=et_user.getText().toString();
                            EditText et_pwd=view1.findViewById(R.id.et_2);
                            String pwd=et_pwd.getText().toString();
                            Toast.makeText(MainActivity4.this,"你的账号为"+user+",你的密码为:"+pwd,Toast.LENGTH_SHORT).show();
                        }
                    });
            builde.show();
        }
    };
}

5.Spinner 下拉框

1.静态

先要在values里定义一个xml文件

<resources>
    <string-array name="depart_arr">
        <item>总经办</item>
        <item>人事部</item>
        <item>研发部</item>
        <item>销售部</item>
    </string-array>
</resources>

之后引用文件

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main5);
    //与按钮结合使用
    but_xlk = findViewById(R.id.but_xlk);
    sp_1=findViewById(R.id.sp_1);
    but_xlk.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String choice=sp_1.getSelectedItem().toString();
            Toast.makeText(MainActivity5.this,"你的选择是:"+choice,Toast.LENGTH_SHORT).show();
        }
    });

2.动态

 sp_2 = findViewById(R.id.sp_2);
    String[] acd={"金水区","惠济区","管城区","二七区","中原区"};
    ArrayAdapter arrayAdapter=new ArrayAdapter(MainActivity5.this, android.R.layout.simple_list_item_1,acd);
    sp_2.setAdapter(arrayAdapter);

    sp_2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            String choice=acd[i];
            Toast.makeText(MainActivity5.this,"你的选择是:"+choice,Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值