xml
<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="点击触发弹出普通对话框" android:onClick="pt_onclick"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="点击触发弹出单选对话框" android:onClick="danxuanonclick"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="点击触发弹出多选对话框" android:onClick="duoxuanonclick"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="点击触发弹出自定义对话框" android:onClick="zdy_onclick"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="点击触发弹出自定义旋转进度条对话框" android:onClick="jdt_onclick"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="点击触发弹出自定义水平进度条对话框" android:onClick="jdt1_onclick"/>
loginlayout.xml
<?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"> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/anniu2" android:scaleType="fitXY"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="用户名" android:id="@+id/et_username"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="密码" android:inputType="textPassword" android:id="@+id/et_password"/> </LinearLayout>
java
package com.example.chenshuai.test322; import android.app.AlertDialog; import android.app.DatePickerDialog; import android.app.ProgressDialog; import android.app.TimePickerDialog; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.ContextMenu; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.SubMenu; import android.view.View; import android.widget.Button; import android.widget.DatePicker; import android.widget.EditText; import android.widget.ProgressBar; import android.widget.SeekBar; import android.widget.TimePicker; import android.widget.Toast; import java.util.Calendar; /** * Created by chenshuai on 2016/3/30. */ public class UIActivity2 extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ui2); Button bt_changan = (Button)findViewById(R.id.bt_changan); bt_changan.setOnCreateContextMenuListener(this); //进度条 //final 生命周期为整个类 final ProgressBar pb_1 = (ProgressBar)findViewById(R.id.pb_1); final ProgressBar pb_2 = (ProgressBar)findViewById(R.id.pb_2); SeekBar sb_1 = (SeekBar)findViewById(R.id.sbr_1); sb_1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { pb_2.setProgress(progress); } //开始拖动 @Override public void onStartTrackingTouch(SeekBar seekBar) { } //结束拖动 @Override public void onStopTrackingTouch(SeekBar seekBar) { pb_2.setProgress(seekBar.getProgress()); if (seekBar.getProgress() == seekBar.getMax()) { //设置不可见 GONE不占位置 invisible 占位置 pb_1.setVisibility(View.GONE); } else { //可见 pb_1.setVisibility(View.VISIBLE); } } }); } //菜单 上下文菜单 @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { menu.add(0, 1, 0, "添加"); menu.add(0, 2, 1, "修改"); menu.add(0, 3, 2, "删除"); SubMenu m = menu.addSubMenu(0, 4, 3, "子菜单"); m.add(0,4-1,0,"子菜单项一"); m.add(0,4-2,1,"子菜单项二"); m.add(0,4-3,2,"子菜单项三"); m.add(0,4-4,3,"子菜单项四"); super.onCreateContextMenu(menu, v, menuInfo); } @Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case 1: Toast.makeText(UIActivity2.this, "触发了添加功能", Toast.LENGTH_SHORT).show(); break; case 2: Toast.makeText(UIActivity2.this, "触发了删除功能", Toast.LENGTH_SHORT).show(); break; case 3: Toast.makeText(UIActivity2.this, "触发了修改功能", Toast.LENGTH_SHORT).show(); break; } return super.onContextItemSelected(item); } @Override //重写 创建选项菜单的方法 public boolean onCreateOptionsMenu(Menu menu) { //添加菜单项 //第一种 纯编码方式 /*menu.add(0, 1, 0, "添加"); menu.add(0, 2, 2, "删除"); menu.add(0, 3, 1, "修改"); */ //第二种 加载菜单文件方式 //1.获取一个菜单加载器 MenuInflater menuInflater = getMenuInflater(); menuInflater.inflate(R.menu.mymenu,menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected (MenuItem item){ //Toast.makeText(UiActivity2.this, "选中的菜单项是" + item.getTitle(), Toast.LENGTH_SHORT).show(); //根据菜单项不同响应不同功能 switch (item.getItemId()) { case 1: Toast.makeText(UIActivity2.this, "触发了添加功能", Toast.LENGTH_SHORT).show(); break; case 2: Toast.makeText(UIActivity2.this, "触发了删除功能", Toast.LENGTH_SHORT).show(); break; case 3: Toast.makeText(UIActivity2.this, "触发了修改功能", Toast.LENGTH_SHORT).show(); break; case R.id.it1: Toast.makeText(UIActivity2.this, "触发了删除一功能", Toast.LENGTH_SHORT).show(); break; case R.id.it2: Toast.makeText(UIActivity2.this, "触发了添加一功能", Toast.LENGTH_SHORT).show(); break; } return super.onOptionsItemSelected(item); } public void pt_onclick(View view) { //普通对话框 //对话框的构建器 //AlertDialog.Builder ab = new AlertDialog.Builder(this); /* ab.setTitle("数据删除"); ab.setMessage("你确定要删除吗?"); //负面按钮 ab.setPositiveButton("取消", null); //正面按钮 ab.setNegativeButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(UIActivity2.this, "删除成功", Toast.LENGTH_SHORT).show(); } }); //显示 ab.show();*/ //方法链调用 new AlertDialog.Builder(this) .setTitle("确认保存") .setMessage("确定要保存吗?") .setPositiveButton("确定", null) .setNegativeButton("取消", null) .show(); } public void danxuanonclick(View v) { final String[] str_color = {"红","绿","蓝","灰"}; new AlertDialog.Builder(this) .setTitle("请选择颜色") .setSingleChoiceItems(str_color, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(UIActivity2.this, "选中了" + str_color[which], Toast.LENGTH_SHORT).show(); //移除 //dialog.dismiss(); } }) .setNeutralButton("确定", null) .show(); } public void duoxuanonclick(View v) { final String[] str_color = {"红","绿","蓝","灰"}; final boolean[] bl_sz = {true,true,true,false}; new AlertDialog.Builder(this) .setTitle("请选择颜色") .setMultiChoiceItems(str_color, bl_sz, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { if (isChecked) { Toast.makeText(UIActivity2.this, str_color[which] + "选中了", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(UIActivity2.this, str_color[which] + "取消选中了", Toast.LENGTH_SHORT).show(); } } }) .setNeutralButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //遍历数组 for (boolean b : bl_sz) { try { Thread.sleep(100); } catch (Exception ex) { } Toast.makeText(UIActivity2.this, "值=" + b, Toast.LENGTH_SHORT).show(); } } }) .show(); } public void zdy_onclick(View view) { //1.获取加载器 LayoutInflater layoutInflater = getLayoutInflater(); //2.用加载器加载文件 //final View v_1 = layoutInflater.inflate(R.layout.loginlayout,null); new AlertDialog.Builder(this) //.setView(v_1) .setView(R.layout.loginlayout) .setNegativeButton("取消", null) .setPositiveButton("登陆", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { AlertDialog al = (AlertDialog) dialog; //处理数据 //EditText et_username = (EditText)v_1.findViewById(R.id.et_username); EditText et_username = (EditText) al.findViewById(R.id.et_username); //Toast.makeText(UIActivity2.this, "用户名="+et_username.getText(), Toast.LENGTH_SHORT).show(); String useneme = et_username.getText().toString(); EditText et_password = (EditText) al.findViewById(R.id.et_password); String pwd = et_password.getText().toString(); if (useneme.equals("wukong") && pwd.equals("123")) { Intent in = new Intent(UIActivity2.this, Activity1.class); startActivity(in); } else Toast.makeText(UIActivity2.this, "用户名" + useneme + "密码" + pwd, Toast.LENGTH_SHORT).show(); { } } }) .show(); } //旋转进度条 public void jdt_onclick(View view) { final ProgressDialog pd = new ProgressDialog(this); pd.setMessage("正在加载请稍后"); pd.show(); //创建thread实例 =【重写run方法 启动多线程 new Thread() { @Override public void run() { super.run(); try { Thread.sleep(3000); } catch (Exception e) { } pd.dismiss();//关闭 } }.start(); } //水平进度条 public void jdt1_onclick(View view) { final ProgressDialog pd = new ProgressDialog(this); pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pd.setMessage("正在加载请稍后"); pd.show(); //创建thread实例 实现Runable接口 启动多线程 new Thread(new Runnable() { @Override public void run() { for (int i = 0; i <= pd.getMax(); i++) { try { Thread.sleep(100); } catch (Exception e) { } pd.setProgress(i); } pd.dismiss(); } }).start(); //创建thread实例 重写run方法 启动多线程 new Thread() { @Override public void run() { super.run(); //新线程 子线程 for (int i = 0; i <= pd.getMax(); i++) { try { Thread.sleep(100); } catch (Exception e) { } pd.setProgress(i); } pd.dismiss();//关闭 //子线程不能直接操作主线程 UI线程 //1.可以用hander //2.也可以用runOnUiThread runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(UIActivity2.this, "加载完成", Toast.LENGTH_SHORT).show(); } }); } }.start(); } }