Android高级控件(下)

满堂花醉三千客,一剑霜寒十四州。这篇文章主要讲述Android高级控件(下)相关的知识,希望能为你提供帮助。
计时器(Chronometer)
getBase()基准时间
setFormat()设置显示格式
start()开始计时
stop()停止计时
setOnChronometerListener计时改变的监听事件

1 < ?xml version="1.0" encoding="utf-8"?> 2 < LinearLayout 3xmlns:android="http://schemas.android.com/apk/res/android" 4android:layout_width="fill_parent" 5android:orientation="vertical" 6android:layout_height="fill_parent"> 7< Chronometer 8android:id="@+id/tmd" 9android:layout_width="wrap_content" 10android:layout_height="wrap_content"/> 11< LinearLayout 12android:orientation="horizontal" 13android:layout_width="fill_parent" 14android:layout_height="fill_parent"> 15< Button 16android:id="@+id/btStart" 17android:layout_width="wrap_content" 18android:layout_height="wrap_content" 19android:text="开始计时"/> 20< Button 21android:id="@+id/btStop" 22android:layout_width="wrap_content" 23android:layout_height="wrap_content" 24android:text="停止计时"/> 25< /LinearLayout> 26 < /LinearLayout>

 
package come.waas; import android.app.Activity; import android.app.Service; import android.os.Bundle; import android.os.SystemClock; import android.os.Vibrator; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Chronometer; public class timer extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.timer); Button bt1 = (Button)findViewById(R.id.btStart); Button bt2 = (Button)findViewById(R.id.btStop); final Chronometer ch = (Chronometer)findViewById(R.id.tmd); //获得震动服务  final Vibrator b = (Vibrator) getApplication() .getSystemService( Service.VIBRATOR_SERVICE); //震动 bt1.setOnClickListener(new OnClickListener() {public void onClick(View v) { ch.start(); //开始计时 b.vibrate(new long[]{1000,1000,1000,2000}//震动频率 , 0); } }); bt2.setOnClickListener(new OnClickListener() {public void onClick(View v) { ch.stop(); //结束计时 ch.setBase(SystemClock.elapsedRealtime()); //复位
} }); } }

 
 
 
TabHost布局
TabActivity类常用方法
getTabHost()获取对象
Layoutinflater类常用方法
inflate(布局管理器ID,组件容器、组件参数,True)
【Android高级控件(下)】LayoutInflater from()从指定容器之中获得LayoutInflater对象
TabHost.TabSpec
setindicator()设置一个Tab
setContent设置要显示的组件ID
package come.waas; import android.app.TabActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.widget.TabHost; import android.widget.TabHost.TabSpec; public class lay extends TabActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TabHost tabHost = getTabHost(); //创建Tabhost对象 LayoutInflater.from(this). inflate(R.layout.lay, tabHost.getTabContentView(), true); TabSpec s1 = tabHost.newTabSpec("tab1")//设置标签的ID .setIndicator("选项一")//设置标签的显示内容 .setContent(R.id.tva); //设置标签的标题 tabHost.addTab(s1); TabSpec s2 = tabHost.newTabSpec("tab2") .setIndicator("选项二") .setContent(R.id.tvb); tabHost.addTab(s2); TabSpec s3 = tabHost.newTabSpec("tab3") .setIndicator("选项三") .setContent(R.id.tvc); tabHost.addTab(s3); } }

 


    推荐阅读