今天的这一篇我们主要讲的就是,标题中提及到的几个知识点,也就是关于Android UI开发的组件开发方面的讲解,我会一一说明和举例,希望对大家有所帮助。
一.Dialog(对话框)
1.简单介绍:
对话框是一种显示于Activity之上的界面元素,常用的对话框有如下几种:
(1)AlertDialog(提示对话框)
(2)ProgressDialog(进度对话框)
(3)DatePickerDialog(日期选择对话框)
(4)TimePickerDialog(时间选择对话框)
2.AlertDialog
(1)MainActivity.java
package com.example.l_0826_dialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
private View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater lf=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
view=lf.inflate(R.layout.cell, null);
//获得AlertDialog静态内部类Builder对象并由该类创建对话框
AlertDialog.Builder ab=new AlertDialog.Builder(this);
ab.setView(view);
//设置对话框的标题
ab.setTitle("温馨提示:");
//设置对话框的内容信息
ab.setMessage("您确定退出应用程序吗?");
//设置对话框的图标
ab.setIcon(R.drawable.ic_launcher);
//给对话框添加选择按钮
ab.setNegativeButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "您已经退出了应用程序", Toast.LENGTH_SHORT).show();
}
});
//用setNeutraButton来设置的按钮优先级高于setNegativeButton按钮,其他没有区别
ab.setNeutralButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity. this,"您取消了退出该应用", Toast.LENGTH_SHORT).show();
}
});
//对话框的显示
ab.show();
}
}
(2)另一种方法:
new AlertDialog.Builder(this).setTitle("温馨提示:").setMessage("Are you sure?")
.setNegativeButton("确定",new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Act2.this, "您已经退出了应用程序", Toast.LENGTH_SHORT).show();
}
})
.setNeutralButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Act2.this, "您取消了退出该应用程序", Toast.LENGTH_SHORT).show();
}
}).show();
(3)运行效果:
2.ProgressDialog
(1)MainActivity.java
package com.example.l0826_progressdialog;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity{
private ProgressDialog pd;
private View view;
private LayoutInflater lf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lf=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
view=lf.inflate(R.layout.cell, null);
pd=new ProgressDialog(this);
pd.setView(view);
pd.setTitle("温馨提示:");
pd.setMessage("正在下载");
pd.setMax(1000);
pd.setButton("返回", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "下载完毕", Toast.LENGTH_SHORT).show();
}
});
//如果不设置这句话的效果如图1,设置了之后效果如图2
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.show();
}
}
(2)运行效果1,未设置setProgressStyle方法:
(3)运行效果2,设置了setPrograssStyle方法
3.DatePickerDialog
(1)MainActivity.java
package com.example.l0826_datepickerdailog;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;
public class MainActivity extends Activity {
private DatePickerDialog dpd;
private TimePickerDialog tpd;
private int year,month,day,hour,minute,second;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
year=2013;
month=7;
day=26;
hour=06;
minute=30;
//DatePickerDialog的使用
dpd=new DatePickerDialog(MainActivity.this, new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
Toast.makeText(MainActivity.this, year+"-"+month+"-"+day, Toast.LENGTH_SHORT).show();
}
}, year, month, day);
dpd.setTitle("闹钟");
dpd.setMessage("日期设置");
dpd.show();
//TimePickerDialog的使用
tpd=new TimePickerDialog(MainActivity.this, new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(MainActivity.this, hour+"/"+minute,Toast.LENGTH_SHORT).show();
}
}, hour, minute, true);
tpd.show();
}
}
(2)运行效果:
二.Menu(菜单)
1.OptionsMenu(选项菜单)
点击MENU键时出现,一般在屏幕下方,最多显示六项,多于六项时可以通过“更多”选项找到;
2.SubMenu(子菜单)
只能扩展基本菜单,但不能再嵌套子菜单,不支持图标的显示;
3.ContextMenu(上下文菜单)
长按屏幕中某一界面元素时,出现的菜单,不支持图标的显示。类似于普通桌面的右击键菜单,但是这里是点击超过两秒而显示的,,上下文菜单可以被注册到任何View对象中,如基本空间、布局控件、ListView和Spinner中的某一项。
需要重写onContextItemSelected方法。
举例如下:
(1)例子1:
package com.example.l0826_optionmenu;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, 1, Menu.NONE,"菜单1");
menu.add(Menu.NONE, 2, Menu.NONE,"菜单2");
menu.add(Menu.NONE, 3, Menu.NONE,"菜单3");
menu.add(Menu.NONE, 4, Menu.NONE,"菜单4");
menu.add(Menu.NONE, 5, Menu.NONE,"菜单5");
SubMenu submenu=menu.addSubMenu(Menu.NONE, 6, Menu.NONE,"菜单6");
submenu.setHeaderIcon(R.drawable.a1);
submenu.setIcon(R.drawable.a2);
submenu.add(Menu.NONE, 7, Menu.NONE,"子菜单7");
submenu.add(Menu.NONE, 8, Menu.NONE,"子菜单8");
submenu.add(Menu.NONE, 9, Menu.NONE,"子菜单9");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
Toast.makeText(MainActivity.this, "您点击了菜单1",Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(MainActivity.this, "您点击了菜单2",Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(MainActivity.this, "您点击了菜单3",Toast.LENGTH_SHORT).show();
break;
case 4:
Toast.makeText(MainActivity.this, "您点击了菜单4",Toast.LENGTH_SHORT).show();
break;
case 5:
Toast.makeText(MainActivity.this, "您点击了菜单5",Toast.LENGTH_SHORT).show();
break;
case 6:
Toast.makeText(MainActivity.this, "您点击了菜单6",Toast.LENGTH_SHORT).show();
break;
case 7:
Toast.makeText(MainActivity.this, "您点击了菜单7",Toast.LENGTH_SHORT).show();
break;
case 8:
Toast.makeText(MainActivity.this, "您点击了菜单8",Toast.LENGTH_SHORT).show();
break;
case 9:
Toast.makeText(MainActivity.this, "您点击了菜单10",Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
(2)例子2:
package com.example.l0826_optionmenu;
import android.os.Bundle;
import android.app.Activity;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView) findViewById(R.id.tv);
registerForContextMenu(tv);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.add(Menu.NONE, 1, Menu.NONE,"菜单1");
menu.add(Menu.NONE, 2, Menu.NONE,"菜单2");
menu.add(Menu.NONE, 3, Menu.NONE,"菜单3");
menu.add(Menu.NONE, 4, Menu.NONE,"菜单4");
menu.add(Menu.NONE, 5, Menu.NONE,"菜单5");
SubMenu submenu=menu.addSubMenu(Menu.NONE, 6, Menu.NONE,"子菜单");
submenu.setHeaderIcon(R.drawable.a1);
submenu.setIcon(R.drawable.a2);
submenu.add(Menu.NONE, 7, Menu.NONE,"子菜单1");
submenu.add(Menu.NONE, 8, Menu.NONE,"子菜单2");
submenu.add(Menu.NONE, 9, Menu.NONE,"子菜单3");
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
Toast.makeText(MainActivity.this, "您点击了菜单1",Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(MainActivity.this, "您点击了菜单2",Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(MainActivity.this, "您点击了菜单3",Toast.LENGTH_SHORT).show();
break;
case 4:
Toast.makeText(MainActivity.this, "您点击了菜单4",Toast.LENGTH_SHORT).show();
break;
case 5:
Toast.makeText(MainActivity.this, "您点击了菜单5",Toast.LENGTH_SHORT).show();
break;
case 6:
Toast.makeText(MainActivity.this, "您点击了菜单6",Toast.LENGTH_SHORT).show();
break;
case 7:
Toast.makeText(MainActivity.this, "您点击了菜单7",Toast.LENGTH_SHORT).show();
break;
case 8:
Toast.makeText(MainActivity.this, "您点击了菜单8",Toast.LENGTH_SHORT).show();
break;
case 9:
Toast.makeText(MainActivity.this, "您点击了菜单10",Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onContextItemSelected(item);
}
}
三.Notification(通知栏)
这一个我们直接看代码:
package com.example.l0826_notification;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建NotificationManager来管理Notification
NotificationManager manager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
//创建Notification的对象,可以直接通过参数设置图标、标题和id号
Notification notification=new Notification(R.drawable.ic_launcher, "天气提示", 0);
PendingIntent pending=PendingIntent.getActivity(MainActivity.this, 0,getIntent(), Intent.FLAG_ACTIVITY_NEW_TASK);
notification.setLatestEventInfo(MainActivity.this, "哈尔滨天气预报", "晴,温暖", pending);
manager.notify(0, notification);
}
@Override
protected void onRestart() {
NotificationManager manager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(0);
super.onRestart();
}
}
四.TagHost(导航标签)
1.方式一:继承TabActivity的导航标签
package com.example.l0826_tabhost;
import android.app.TabActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat.Action;
import android.view.LayoutInflater;
import android.widget.TabHost;
public class MainActivity extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost th=getTabHost();
LayoutInflater.from(this).inflate(R.layout.activity_main, th.getTabContentView(), true);
th.addTab(th.newTabSpec("Tab1").setIndicator("导航1").setContent(new Intent(MainActivity.this,Act2.class)));
th.addTab(th.newTabSpec("Tab2").setIndicator("导航2").setContent(R.id.layout));
}
}
2.方式二:自定义TabHost
package com.example.jiuzhouzhanji;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
public class Act_001 extends Activity{
private TabHost tabHost;
private TabWidget tabWidget;
private int p_w_picpathA[]={R.drawable.b007,R.drawable.b008,R.drawable.b006,R.drawable.b005};
private int p_w_picpathB[]={R.drawable.b007_1,R.drawable.b008_1,R.drawable.b006_1,R.drawable.b005_1};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_001);
tabHost=(TabHost)findViewById(R.id.tabhost);
tabHost.setup();
tabWidget=tabHost.getTabWidget();
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator(createTabView(tabHost, p_w_picpathA[0])).setContent(R.id.tab1));
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator(createTabView(tabHost, p_w_picpathA[1])).setContent(R.id.tab2));
tabHost.addTab(tabHost.newTabSpec("tab3")
.setIndicator(createTabView(tabHost, p_w_picpathA[2])).setContent(R.id.tab3));
tabHost.addTab(tabHost.newTabSpec("tab4")
.setIndicator(createTabView(tabHost, p_w_picpathA[3])).setContent(R.id.tab4));
showSpec();
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
showSpec();
}
});
}
public void showSpec(){
for(int i=0;i<tabWidget.getChildCount();i++){
View view=tabWidget.getChildAt(i);
if(tabHost.getCurrentTab()==i){
view.setBackgroundResource(p_w_picpathA[i]);
}else {
view.setBackgroundResource(p_w_picpathB[i]);
}
}
}
public View createTabView (TabHost tabHost,int tabImage){
View tabView=LayoutInflater.from(this).inflate(R.layout.cell, null);
ImageView iv=(ImageView)tabView.findViewById(R.id.iv);
iv.setBackgroundResource(tabImage);
return tabView;
}
}
转载于:https://blog.51cto.com/wangzhaoli/1283027