代码块:
public class MainActivity extends AppCompatActivity{
private EditText et1,et2;
private AlertDialog ad;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
String name="我是张三";
public void click(View view){
switch (view.getId()){
case R.id.btn1:
new AlertDialog.Builder(this)//在本页面创建出一个弹出框
.setTitle("标题")//设置标题
.setIcon(R.mipmap.ic_launcher)//设置标题的图片
.setMessage(name).setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,name,Toast.LENGTH_SHORT).show();
}
}).show();//show():弹出Dialig
break;
case R.id.btn2:
new AlertDialog.Builder(this)//在本页面创建出一个弹出框
.setTitle("标题")//设置标题
.setIcon(R.mipmap.ic_launcher)//设置标题的图片
.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//在这做删除的工作
Toast.makeText(MainActivity.this,"信息已删除",Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("取消",null)//取消按钮设置
.show();
break;
case R.id.btn3:
final String[] arr={"牛蛙","田鸡","蛤蟆","蝎子"};
new AlertDialog.Builder(this)//在本页面创建出一个弹出框
.setTitle("标题")//设置标题
.setIcon(R.mipmap.ic_launcher)//设置标题的图片
.setSingleChoiceItems(arr, 1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Toast.makeText(MainActivity.this,arr[which],Toast.LENGTH_SHORT).show();
}
}).show();
break;
case R.id.btn4:
final String[] arr1={"牛蛙","田鸡","蛤蟆","蝎子"};
final StringBuilder sb=new StringBuilder();
new AlertDialog.Builder(this)//在本页面创建出一个弹出框
.setTitle("标题")//设置标题
.setIcon(R.mipmap.ic_launcher)//设置标题的图片
.setMultiChoiceItems(arr1, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
//判断当前选项是否是选中状态,若是,就取出值放到sb,如果不选中,就把当前值从sb里删除掉
if(isChecked){
sb.append(arr1[which]);
}else {
int start=sb.indexOf(arr1[which]);
sb.delete(start,start+arr1[which].length());
}
}
}).setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Toast.makeText(MainActivity.this,sb.toString(),Toast.LENGTH_SHORT).show();
}
}).show();
break;
case R.id.btn5:
final ProgressDialog progressDialog = new ProgressDialog(this);//进度提示框
progressDialog.setTitle("下载进度");
progressDialog.setIcon(R.mipmap.ic_launcher);
progressDialog.setMessage("当前正在下载中……");
progressDialog.setMax(100);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置为横的进度条
progressDialog.show();
final Handler han=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what==110){
progressDialog.setProgress(msg.arg1);
if(msg.arg1==100){
progressDialog.dismiss();
}
}
}
};
new Thread(new Runnable() {
@Override
public void run() {
for (int i=0;i<=100;i+=10){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Message mess=Message.obtain();
mess.arg1=i;
mess.what=110;
han.sendMessage(mess);
}
}
}).start();
break;
case R.id.btn6:
break;
case R.id.btn7:
break;
case R.id.btn8:
break;
}
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
tools:context="org.frxm.day20_dialog.MainActivity">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="信息提示框"
android:onClick="click"/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确认框"
android:onClick="click"/>
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="单选框"
android:onClick="click"/>
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="多选框"
android:onClick="click"/>
<Button
android:id="@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="进度框"
android:onClick="click"/>
<Button
android:id="@+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="列表框"
android:onClick="click"
/>
<Button
android:id="@+id/btn7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="日期对话框"
android:onClick="click"
/>
<Button
android:id="@+id/btn8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自定义对话框"
android:onClick="click"
/>
</LinearLayout>
Login布局:
<?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:orientation="vertical" >
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="手机号/邮箱"
android:id="@+id/et_name"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入密码"
android:inputType="numberPassword"
android:id="@+id/et_pwd"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp">
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="登陆"
android:background="@color/colorAccent"
android:layout_marginRight="20dp"
android:id="@+id/btn_login"/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="取消"
android:layout_marginRight="20dp"
android:id="@+id/btn_cancel"/>
</LinearLayout>
</LinearLayout>