歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android開發之進度條、拖動條、TabHost

Android開發之進度條、拖動條、TabHost

日期:2017/3/1 10:45:16   编辑:Linux編程

進度條(ProgressBar)

進度條可以帶給用戶良好的體驗,Android系統已經為我們提供了ProgressBar類來完成進度條的效果,我們可以很方便的運用該類。其中最常見的兩種進度條是“環形進度條”和“水平進度條”,在布局文件中定義兩種進度條的方式比較相似,區別是,定義“水平進度條時”需要加上一項屬性“”。ProgressBar類

中常用的方法如下:

ProgressBar.setMax(int max);設置總長度為100

ProgressBar.setProgress(int progress);設置已經開啟長度為0,假設設置為50,進度條將進行到一半停止。

實例:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="進度條演示" />

<ProgressBar

android:id="@+id/probarId1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:max="1000"

android:progress="100" />

<ProgressBar

android:id="@+id/probarId2"

android:layout_marginTop="50dp"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:max="100000"

android:progress="100"

android:secondaryProgress="300"

/>

</LinearLayout>

////////////////// ProgressBarActivity///////////////////

package cn.class3g.activity;

import android.app.Activity;

import android.os.Bundle;

import android.os.Handler;

import android.util.Log;

import android.widget.ProgressBar;

public class ProgressBarActivity extends Activity {

ProgressBar pro = null;

int i = 0;

int proMax = 0;

Handler handler = new Handler();

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

findViews();

}

private void findViews() {

pro = (ProgressBar) this.findViewById(R.id.probarId2);

proMax = pro.getMax();

new Thread(new Runnable() {

public void run() {

while (i < proMax) {

i=dowork();

handler.post(new Runnable() {

public void run() {

pro.setProgress(i);

}

});

}

}

}

).start();

}

public int dowork(){

Log.d("tag", String.valueOf(i));

return i+=1;

}

/*

* //不能用

*

* @Override public void run() { // TODO Auto-generated method stub while

* (i++ < proMax) { pro.setProgress(i); try { Thread.sleep(50); } catch

* (InterruptedException e) { // TODO Auto-generated catch block

* e.printStackTrace(); } } }

*/

}

拖動條(SeekBar)

進度條只能顯示進度,用戶不能與程序交互,通過對其操作來改變其值。而拖動條就可以實現此功能。拖動條比較常見,如“千千靜聽”中的播放進度條就是一個拖動條。Android平台中的SeekBar 是ProgressBar 的間接子類。

SeekBar.getProgress():獲取拖動條當前值

調用setOnSeekBarChangeListener() 方法,處理拖動條值變化事件, 把SeekBar.OnSeekBarChangeListener 實例作為參數傳入

實例:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

<SeekBar

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:max="1000"

android:id="@+id/seekbarId"

/>

</LinearLayout>

////////////////// SeekBarActivity/////////////////

package cn.class3g.activity;

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.widget.SeekBar;

import android.widget.SeekBar.OnSeekBarChangeListener;

public class SeekBarActivity extends Activity implements OnSeekBarChangeListener{

SeekBar seekbar = null;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.seekbar);

findViews();

}

private void findViews() {

seekbar = (SeekBar) this.findViewById(R.id.seekbarId);

seekbar.setOnSeekBarChangeListener(this);

}

@Override

public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {

Log.d("tag","changed:" +String.valueOf(arg0.getProgress()));

}

@Override

public void onStartTrackingTouch(SeekBar seekBar) {

Log.d("tag","start:" +String.valueOf(seekBar.getProgress()));

}

@Override

public void onStopTrackingTouch(SeekBar seekBar) {

Log.d("tag","stop:" +String.valueOf(seekBar.getProgress()));

}

}

TabHost

<?xml version="1.0" encoding="utf-8"?>

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent" >

<LinearLayout

android:id="@+id/tab1"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

<Button

android:id="@+id/buttonId"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="切換到tab2" />

<ImageView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/p2"

android:scaleType="fitCenter"

android:layout_marginTop="20dp"

/>

</LinearLayout>

</TabHost>

/////////////// TabHostActivity///////////

package cn.class3g.activity;

import android.app.TabActivity;

import android.content.Intent;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.TabHost;

public class TabHostActivity extends TabActivity {

TabHost tabHost = null;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

tabHost = this.getTabHost();

LayoutInflater inflater = LayoutInflater.from(this);

inflater.inflate(R.layout.tabhost, tabHost.getTabContentView(), true);

// tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator(""));

tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("切換標簽")

.setContent(R.id.tab1));

tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("標簽2")

.setContent(new Intent(this, ProgressBarActivity.class)));

findViews();

}

private void findViews() {

Button btn = (Button) this.findViewById(R.id.buttonId);

btn.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// tabHost.setCurrentTab(1);

tabHost.setCurrentTabByTag("tab2");

}

});

}

}

Copyright © Linux教程網 All Rights Reserved