.xml
<?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:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.hanqi.application3.UIActivity2" android:orientation="vertical"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="长按触发上下文菜单" android:id="@+id/bt_changan"/> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/pb_1" android:visibility="gone"/> <ProgressBar android:layout_width="match_parent" android:layout_height="wrap_content" style="?android:attr/progressBarStyleHorizontal" android:max="100" android:progress="50" android:secondaryProgress="70" android:id="@+id/pb_2" android:visibility="gone"/> <!--progress 当前进度--> <!-- <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" style="@android:style/Widget.ProgressBar.Small"/> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" style="@android:style/Widget.ProgressBar.Large"/> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" style="@android:style/Widget.ProgressBar.Inverse"/> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" style="@android:style/Widget.ProgressBar.Large.Inverse"/> --> <SeekBar android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="20" android:secondaryProgress="40" android:id="@+id/sb_1"/> <!--<RatingBar android:layout_width="wrap_content" android:layout_height="wrap_content" android:numStars="6" android:rating="3" android:isIndicator="true"/> numStars 设置几颗星 isIndicator 是否不准修改--> <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="dx_onClick" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="点击触发弹出多选对话框" android:onClick="ddx_onClick" /> <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" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="点击触发日期对话框" android:onClick="rq_onClick" />ri <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="点击触发时间对话框" android:onClick="sj_onClick" /> </LinearLayout>
.java
package com.hanqi.application3; import android.app.Activity; import android.app.AlertDialog; import android.app.DatePickerDialog; import android.app.ProgressDialog; import android.app.TimePickerDialog; import android.content.DialogInterface; import android.os.Bundle; 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; public class UIActivity2 extends Activity { @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); //bt_changan.setOnContextClickListener();创建监听 //进度条 //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.sb_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()) { //设置不可见INVISIBLE 占有空间,但是隐藏 //GONE 直接取消 pb_1.setVisibility(View.GONE); } else { //可见VISIBLE 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,41,0,"子菜单项1"); m.add(0,42,0,"子菜单项2"); m.add(0,43,0,"子菜单项3"); // MenuInflater mi = getMenuInflater(); // mi.inflate(R.menu.mymenu, menu); super.onCreateContextMenu(menu, v, menuInfo); } @Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.it2: Toast.makeText(UIActivity2.this, "触发了添加1功能", Toast.LENGTH_SHORT).show(); break; case R.id.it1: Toast.makeText(UIActivity2.this, "触发了删除1功能", 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 mi = getMenuInflater(); mi.inflate(R.menu.mymenu, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { //item.getTitle() 加载标题 //Toast.makeText(UIActivity2.this, "选中的菜单项是"+item.getTitle(), Toast.LENGTH_SHORT).show(); //根据菜单项的不同相应不同的功能 //获取Item的id 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, "触发了添加1功能", Toast.LENGTH_SHORT).show(); break; case R.id.it2: Toast.makeText(UIActivity2.this, "触发了删除1功能", Toast.LENGTH_SHORT).show(); break; } return super.onOptionsItemSelected(item); } public void pt_onClick(View v) { // //构建一个普通对话框 // //对话框的构建器 // AlertDialog.Builder ab = new AlertDialog.Builder(this); // ab.setTitle("数据删除"); // ab.setMessage("你确定要删除码?"); // //负面按钮 // ab.setNeutralButton("取消",null); // //正面按钮 // ab.setPositiveButton("确定", 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("你真的要保存吗?") .setNeutralButton("取消",null) .setPositiveButton("保存",null) .show(); } public void dx_onClick(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 ddx_onClick(View v) { final String[] str_color={"红","绿","黄","蓝"}; final boolean[] bl_xz = {true,false,false,true}; new AlertDialog.Builder(this) .setTitle("请选择颜色") .setMultiChoiceItems(str_color, bl_xz, new DialogInterface.OnMultiChoiceClickListener() { @Override //是否选中isChecked public void onClick(DialogInterface dialog, int which, boolean isChecked) { if (isChecked) { Toast.makeText(UIActivity2.this, str_color + "被选中", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(UIActivity2.this, str_color + "取消选中", Toast.LENGTH_SHORT).show(); } } }) .setNeutralButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //遍历 for (boolean b : bl_xz) { Toast.makeText(UIActivity2.this, "值= " + b, Toast.LENGTH_SHORT).show(); } } }) .show(); } public void zdy_onClick(View v) { //1.获取加载器 LayoutInflater lif = getLayoutInflater(); //2.用加载器加载文件 final View v_1 = lif.inflate(R.layout.loginlayout, null); new AlertDialog.Builder(this) //.setView(R.layout.loginlayout) .setView(v_1) .setNeutralButton("取消", null) .setPositiveButton("登陆", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { AlertDialog al = (AlertDialog) dialog; //处理数据 EditText et_username = (EditText) al.findViewById(R.id.et_username); Toast.makeText(UIActivity2.this, "用户名=" + et_username.getText(), Toast.LENGTH_SHORT).show(); } }) .show(); } public void jdt_onClick(View v) { final ProgressDialog pd = new ProgressDialog(this); pd.setMessage("正在加载,请稍候..."); pd.setCanceledOnTouchOutside(false); // pd.setTitle("进度对话框"); pd.show(); //创建Thread实例重写run(),start()启动多线程 // new Thread() { @Override public void run() { super.run(); try { Thread.sleep(3000); } catch (Exception e) { } pd.dismiss();//关闭 //子线程不能直接操作主线程UI线程 //可以用Thread //runOnUiThread runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(UIActivity2.this, "加载完成...", Toast.LENGTH_SHORT).show(); } }); } }.start(); //多线程 } public void jdt1_onClick(View v) { final ProgressDialog pd = new ProgressDialog(this); pd.setCanceledOnTouchOutside(false); pd.setMessage("正在加载..."); pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pd.show(); //创建Thread实例,实现Runnable接口 ),start()启动多线程 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); } } }).start(); } public void rq_onClick(View v) { //获取当前日期 //单例模式,设计模式的一种 Calendar cc= Calendar.getInstance(); DatePickerDialog ddp = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { Toast.makeText(UIActivity2.this, year+"-"+(monthOfYear+1)+"-"+dayOfMonth, Toast.LENGTH_SHORT).show(); } },cc.get(Calendar.YEAR),cc.get(Calendar.MONTH),cc.get(Calendar.DAY_OF_MONTH)); ddp.show(); } public void sj_onClick(View v) { //获取当前日期 //单例模式,设计模式的一种 Calendar cc= Calendar.getInstance(); TimePickerDialog tpd = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { } },cc.get(Calendar.HOUR),cc.get(Calendar.MINUTE),true); tpd.show(); } }