android中进度条和选项卡的使用

随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)

Activity:

package com.lovo.activity;

import android.app.TabActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ProgressBar;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TimePicker;

import com.lovo.R;

public class TabHostActivity extends TabActivity {
	// 进度条增加的数量
	public int index = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// 设置标题栏的进度条(无刻度的进度条)
		// requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		// 设置标题栏的进度条(有刻度的进度条)
		requestWindowFeature(Window.FEATURE_PROGRESS);

		// 获取tableHost对象
		TabHost tabHost = getTabHost();
		// 将TabHost的布局文件内容加载到TabContentView中
		getLayoutInflater().inflate(R.layout.tab_main,
				tabHost.getTabContentView());
		// 增加Tab;newTabSpec设置选项卡的标识(唯一性);setIndicator设置选项卡上的名称;setContent设置选项卡的内容
		tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("选项一")
				.setContent(R.id.tab_linear1));
		tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("选项二")
				.setContent(R.id.contend_main));
		tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("选项三")
				.setContent(R.id.tab_linear3));
		// 获得按钮对象Button
		Button btn = (Button) findViewById(R.id.tab_btn);
		// 获得日期对象DatePicker
		final DatePicker datePicker = (DatePicker) findViewById(R.id.tab_date);
		// 获得文本框对象TextView
		final TextView tv = (TextView) findViewById(R.id.tab_tx);
		// 获得时间对象TimePicker
		TimePicker timePicker = (TimePicker) findViewById(R.id.tab_time);
		// 设置为24小时制
		timePicker.setIs24HourView(true);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 获得进度条
				ProgressBar pb = (ProgressBar) findViewById(R.id.tab_progress);
				index += 100;
				// 增加进度条的百分比
				pb.incrementProgressBy(1);
				// 将标题栏的进度条显示出来
				setProgressBarVisibility(true);
				// 设置标题栏进度条的增加数
				setProgress(index);
				// 得到日期对象的年月日
				int year = datePicker.getYear();
				int month = datePicker.getMonth();
				int day = datePicker.getDayOfMonth();
				// 将年月日显示在文本框中
				tv.setText(year + "年" + (month + 1) + "月" + day);
			}
		});

	}

}


选项卡XML:tab_main.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/tab_linear1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <ProgressBar
            android:id="@+id/tab_progress"
            style="@android:style/Widget.ProgressBar.Horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="100" />

        <Button
            android:id="@+id/tab_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="点击我" />

        <DatePicker
            android:id="@+id/tab_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TimePicker
            android:id="@+id/tab_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/tab_tx"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <include layout="@layout/content_main" />

    <LinearLayout
        android:id="@+id/tab_linear3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="我的第三个选项卡" />
    </LinearLayout>

</TabHost>


选项卡中引用的XML(content_main.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/contend_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:autoLink="web"
        android:text="网址:http://www.baidu.com" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:autoLink="email"
        android:text="email:abc@sina.com" />

    <!-- 如果使用autoLink="all"表示自动匹配所有 -->

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:autoLink="phone"
        android:text="电话:13888888888" />

    <AutoCompleteTextView
        android:id="@+id/autoText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>


附上图片效果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值