当前位置: 代码迷 >> Android >> android UI总结(五)
  详细解决方案

android UI总结(五)

热度:10   发布时间:2016-04-28 03:38:20.0
android UI小结(五)
一、ScrollView 滚动视图
由FrameLayout派生而出,用于为普通组件添加滚动条的组件。它最多只能包含一个组件,ScrollView的作用就是为该组件添加垂直滚动条。(如果需要添加水平滚动条,则可借助HorizontalScrollView来实现,用法基本相似。)
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"	android:layout_width="match_parent"	android:layout_height="match_parent"><!-- 定义HorizontalScrollView,为里面的组件添加水平滚动条 -->	<HorizontalScrollView	android:layout_width="fill_parent" 	android:layout_height="wrap_content"><LinearLayout android:orientation="vertical"	android:layout_width="match_parent"	android:layout_height="fill_parent">.......</LinearLayout></HorizontalScrollView></ScrollView>


二、Notification 手机状态栏的通知
程序一般通过NotificationManager服务来发送Notification(如未读微信在状态栏的提示):
用法示例:
// 获取系统的NotificationManager服务NotificationManager nm  = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);// 创建一个启动其他Activity的Intent(必须定义OtherActivity)		Intent intent = new Intent(NotificationTest.this			, OtherActivity.class);		PendingIntent pi = PendingIntent.getActivity(			NotificationTest.this, 0, intent, 0);		Notification notify = new Notification.Builder(this)			// 设置打开该通知,该通知自动消失			.setAutoCancel(true)			// 设置显示在状态栏的通知提示信息			.setTicker("有新消息")			// 设置通知的图标			.setSmallIcon(R.drawable.notify)			// 设置通知内容的标题			.setContentTitle("您有新的消息")			// 设置通知内容			.setContentText("XXX给您发送了3条消息")			// // 设置使用系统默认的声音、默认LED灯			// .setDefaults(Notification.DEFAULT_SOUND			// |Notification.DEFAULT_LIGHTS)			// 设置通知的自定义声音			.setSound(Uri.parse("android.resource:R.raw.msg))			.setWhen(System.currentTimeMillis())			// 设改通知将要启动程序的Intent			.setContentIntent(pi).build();		// 发送通知		nm.notify(NOTIFICATION_ID, notify);


三、对话框
1.AlertDialog
由AlertDialog生成的对话框分为图标区、标题区、内容区和按钮区4个区域。
构建一个对话框需要经过以下步骤:
eg1.简单对话框
AlertDialog.Builder builder = new AlertDialog.Builder(this)			// 设置对话框标题			.setTitle("简单对话框")			// 设置图标			.setIcon(R.drawable.tools)			.setMessage("对话框的测试内容\n第二行内容");		// 为AlertDialog.Builder添加【确定】按钮		setPositiveButton(builder);		// 为AlertDialog.Builder添加【取消】按钮		setNegativeButton(builder)			.create()			.show();

其中,setPositiveButton和setNegativeButton方法的代码如下:
private AlertDialog.Builder setPositiveButton(			AlertDialog.Builder builder)	{		// 调用setPositiveButton方法添加确定按钮		return builder.setPositiveButton("确定", new OnClickListener()		{			@Override			public void onClick(DialogInterface dialog, int which)			{				show.setText("单击了【确定】按钮!");			}		});	}private AlertDialog.Builder setNegativeButton(			AlertDialog.Builder builder)	{		// 调用setNegativeButton方法添加取消按钮		return builder.setNegativeButton("取消", new OnClickListener()		{			@Override			public void onClick(DialogInterface dialog, int which)			{				show.setText("单击了【取消】按钮!");			}		});	}

eg2.简单列表项对话框
String[] items = new String[] {"xxx","xxx","xxx","xxx",...  };AlertDialog.Builder builder = new AlertDialog.Builder(this)			// 设置对话框标题			.setTitle("简单列表项对话框")			// 设置图标			.setIcon(R.drawable.tools)			// 设置简单的列表项内容			.setItems(items, new OnClickListener()			{				@Override				public void onClick(DialogInterface dialog, int which)				{					show.setText("你选中了《" + items[which] + "》");				}			});		// 为AlertDialog.Builder添加【确定】按钮		setPositiveButton(builder);		// 为AlertDialog.Builder添加【取消】按钮		setNegativeButton(builder)			.create()			.show();

eg3.单选列表项对话框
AlertDialog.Builder builder = new AlertDialog.Builder(this)			// 设置对话框标题			.setTitle("单选列表项对话框")			// 设置图标			.setIcon(R.drawable.tools)			// 设置单选列表项,默认选中第二项(索引为1)			.setSingleChoiceItems(items, 1, new OnClickListener()			{				@Override				public void onClick(DialogInterface dialog, int which)				{					show.setText("你选中了《" + items[which] + "》");				}			});		// 为AlertDialog.Builder添加【确定】按钮		setPositiveButton(builder);		// 为AlertDialog.Builder添加【取消】按钮		setNegativeButton(builder)			.create()			.show();

eg4.多选列表项的对话框
AlertDialog.Builder builder = new AlertDialog.Builder(this)			// 设置对话框标题			.setTitle("多选列表项对话框")			// 设置图标			.setIcon(R.drawable.tools)			// 设置多选列表项,设置勾选第2项、第4项			.setMultiChoiceItems(items			, new boolean[]{false , true ,false ,true}, null);		// 为AlertDialog.Builder添加【确定】按钮		setPositiveButton(builder);		// 为AlertDialog.Builder添加【取消】按钮		setNegativeButton(builder)			.create()			.show();

eg5.自定义列表项的对话框
AlertDialog.Builder builder = new AlertDialog.Builder(this)			// 设置对话框标题			.setTitle("自定义列表项对话框")			// 设置图标			.setIcon(R.drawable.tools)			// 设置自定义列表项			.setAdapter(new ArrayAdapter<String>(this 					, R.layout.array_item 					, items), null);		// 为AlertDialog.Builder添加【确定】按钮		setPositiveButton(builder);		// 为AlertDialog.Builder添加【取消】按钮		setNegativeButton(builder)			.create()			.show();

eg6.自定义view的对话框
step1.先定义一个页面布局
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"	android:id="@+id/loginForm"	android:orientation="vertical"	android:layout_width="fill_parent"	android:layout_height="fill_parent"	><TableRow><TextView	android:layout_width="fill_parent"	android:layout_height="wrap_content"	android:text="用户名:"	android:textSize="10pt"	/><!-- 输入用户名的文本框 --><EditText	android:layout_width="fill_parent"	android:layout_height="wrap_content"	android:hint="请填写登录帐号"	android:selectAllOnFocus="true"	/></TableRow><TableRow><TextView	android:layout_width="fill_parent"	android:layout_height="wrap_content"	android:text="密码:"	android:textSize="10pt"		/><!-- 输入密码的文本框 -->	<EditText	android:layout_width="fill_parent"	android:layout_height="wrap_content"	android:hint="请填写密码"		android:password="true"	/></TableRow><TableRow><TextView	android:layout_width="fill_parent"	android:layout_height="wrap_content"	android:text="电话号码:"	android:textSize="10pt"		/><!-- 输入电话号码的文本框 -->		<EditText	android:layout_width="fill_parent"	android:layout_height="wrap_content"	android:hint="请填写您的电话号码"	android:selectAllOnFocus="true"	android:phoneNumber="true"	/></TableRow></TableLayout>

step2.创建对话框
//装载/res/layout/login.xml界面布局		TableLayout loginForm = (TableLayout)getLayoutInflater()			.inflate( R.layout.login, null);				new AlertDialog.Builder(this)			// 设置对话框的图标			.setIcon(R.drawable.tools)			// 设置对话框的标题			.setTitle("自定义View对话框")			// 设置对话框显示的View对象			.setView(loginForm)			// 为对话框设置一个“确定”按钮			.setPositiveButton("登录" , new OnClickListener()			{				@Override				public void onClick(DialogInterface dialog,						int which)				{					// 此处可执行登录处理				}			})			// 为对话框设置一个“取消”按钮			.setNegativeButton("取消", new OnClickListener()			{				@Override				public void onClick(DialogInterface dialog,						int which)				{					// 取消登录,不做任何事情。				}			})			// 创建、并显示对话框			.create()			.show();


四、DatePickerDialog、TimePickerDialog
用法不难,例如:
Calendar c = Calendar.getInstance();// 直接创建一个DatePickerDialog对话框实例,并将它显示出来				new DatePickerDialog(DateDialogTest.this,					// 绑定监听器					new DatePickerDialog.OnDateSetListener()					{						@Override						public void onDateSet(DatePicker dp, int year,							int month, int dayOfMonth)						{							EditText show = (EditText) findViewById(R.id.show);							show.setText("您选择了:" + year + "年" + (month + 1)								+ "月" + dayOfMonth + "日");						}					}				//设置初始日期				, c.get(Calendar.YEAR)				, c.get(Calendar.MONTH)				, c.get(Calendar.DAY_OF_MONTH)).show();			}

如果是TimePicker,相应监听器为:
new TimePickerDialog(DateDialogTest.this,					// 绑定监听器					new TimePickerDialog.OnTimeSetListener()					{						@Override						public void onTimeSet(TimePicker tp, int hourOfDay,							int minute)						{//more code here....				//设置初始时间				, c.get(Calendar.HOUR_OF_DAY)				, c.get(Calendar.MINUTE)				//true表示采用24小时制				, true).show();			}


五、ProgressDialog 进度对话框
用法如下:
//第一种方法:调用静态方法显示环形进度条		ProgressDialog.show(this, "任务执行中"			, "任务执行中,请等待", false, true);//第二种方法ProgressDialog pd = new ProgressDialog(ProgressDialogTest.this);// 设置对话框的标题		pd.setTitle("任务正在执行中");		// 设置对话框显示的内容		pd.setMessage("任务正在执行中,敬请等待...");		// 设置对话框能用“取消”按钮关闭		pd.setCancelable(true);		// 设置对话框的进度条风格		pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);	// 设置对话框的进度条是否显示进度:false->显示;true->不显示		pd.setIndeterminate(true);		pd.show(); //②
  相关解决方案