Android高级控件(下)
Chronometer计时器
常用的方法
getBase() 基准时间
setFormat 设置显示格式
start() 开始计时
stop() 停止计时
setOnChronometerListener 计时改变的监听事件
<uses-permission android:name="android.permission.VIBRATE"></uses-permission> 获取震权限
<!--xml代码-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Chronometer android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/cm" android:text="计时器" /> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/bt1" android:text="开始计时"></Button> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/bt2" android:text="结束计时"></Button> </LinearLayout> </LinearLayout>
public class cm extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn1=(Button)findViewById(R.id.bt1);
Button btn2=(Button)findViewById(R.id.bt2);
final Chronometer cm=(Chronometer)findViewById(R.id.cm);
final Vibrator vb=(Vibrator) getApplication()
.getSystemService(Service.VIBRATOR_SERVICE);//获得震动服务
btn1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
cm.start();//开始计时
vb.vibrate(new long[]{1000,1000,1000,2000}, 0);//震动频率
}
});
btn2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
cm.stop();//结束计时
cm.setBase(SystemClock.elapsedRealtime());//复位
vb.cancel();//关闭震动
}
});
}
}
TabHost布局
TabActivity类常用方法
getTabHost() 获取对象
Layoutinflater类常用方法
inflate(布局管理器ID,组件容器、组件参数,True)
LayoutInflater from() 从指定容器之中获得LayoutInflater对象
TabHost.TabSpec
setindicator() 设置一个Tab
setContent 设置要显示的组件ID
public class TH extends TabActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
TabHost thHost=getTabHost();//创建Tabhost对象
LayoutInflater.from(this).inflate(R.layout.tb, thHost.getTabContentView()
,true);
TabSpec s1=thHost.newTabSpec("tab1")//设置标签的ID
.setContent(R.id.TextView01)//设置标签的显示内容
.setIndicator("第一页");//设置标签的标题
thHost.addTab(s1);
TabSpec s2=thHost.newTabSpec("tab2")
.setContent(R.id.TextView02)
.setIndicator("第二页");
thHost.addTab(s2);
TabSpec s3=thHost.newTabSpec("tab3")
.setContent(R.id.TextView03)
.setIndicator("第三页");
thHost.addTab(s3);
}
}
菜单
Menu 父类接口,用于创建主菜单
SubtextMenu 继承Menu接口,用于创建子菜单
ContextMenu 接口继承Menu接口,用户创建上下文菜单
MenuItem 接口用户创建菜单项
onCreateOptionsMenu(Menu menu); 在此方法中设置多个菜单项(返回true表示显示菜单,反之不显示)
onOptionsItemSelected(MenuItem item); 判断菜单项的操作
onOptionsMenuClosed(Menu menu); 当菜单关闭时触发
onPrepareOptionsMenu(Menu menu) 在菜单显示前触发此操作(返回true继续调用oncreateOptionsMenu()方法,反之则不调用)
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, 0, 1, "保存");
menu.add(Menu.NONE, 1, 0, "删除");
SubMenu h=menu.addSubMenu(Menu.NONE, 2, 0, "帮助");
h.add("保存帮助");
h.add("删除帮助");
return super.onCreateOptionsMenu(menu);
}
Gallery控件
Gallery 创建Gellery对象
setSpacing android:spacing 设置两个图片的显示间距
setAdapter 设置图片集
setGravity android:gravity 设置图片的对其方式
public class Galy extends Activity {
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.galy);
final ImageView iView=(ImageView)findViewById(R.id.ImageView01);
Gallery gallery=(Gallery)findViewById(R.id.Gallery01);
final int [] imgid={R.drawable.ps,R.drawable.ie,R.drawable.kg};
List<Map<String, Integer>> list=new ArrayList<Map<String,Integer>>();
for (int i = 0; i < imgid.length; i++) {
Map<String,Integer> map=new HashMap<String,Integer>();
map.put("img",imgid[i]);
list.add(map);
}
SimpleAdapter adapter=new SimpleAdapter
(this,list,R.layout.galy,new String[]{"img"},new int[]{R.id.ImageView01});
gallery.setAdapter(adapter);
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
iView.setImageResource(imgid[arg2]);
}
});
}
}
浙公网安备 33010602011771号