首页 > 代码库 > Android高级控件(下)

Android高级控件(下)

计时器(Chronometer)

  getBase()   基准时间

  setFormat()  设置显示格式

  start()  开始计时

  stop()  停止计时

  setOnChronometerListener  计时改变的监听事件

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

    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);
        
    }
}

 

Android高级控件(下)