Bar的使用方法

沈永新
2023-12-01

一、测试使用的xml文件

<?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" >

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/prs_button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button1" />

    <Button
        android:id="@+id/prs_button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button2" />

    <SeekBar
        android:id="@+id/seek_bar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <RatingBar
        android:id="@+id/rating_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>


1、ProgressBar为普通的

2、可以拖动的进度条

3、星星评分的进度条

代码区:

package com.example.linear_test;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends Activity {
	
	private ProgressBar progressBar1;
	private Button button1, button2;
	private SeekBar seekBar1;
	private RatingBar ratingBar1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.proess_test);
        progressBar1 = (ProgressBar)findViewById(R.id.progressBar1);
        progressBar1.setMax(100);
        button1 = (Button)findViewById(R.id.prs_button1);
        button2 = (Button)findViewById(R.id.prs_button2);
        progressBar1.setProgress(23); //设置第一进度条
        progressBar1.setSecondaryProgress(88); //设置第二进度条
        button1.setOnClickListener(onclickLis);
        button2.setOnClickListener(onclickLis);
        
        setContentView(R.layout.seek_rat_bar);
        seekBar1 = (SeekBar)findViewById(R.id.seek_bar1);
        seekBar1.setMax(100);
        seekBar1.setProgress(60);
        seekBar1.setOnSeekBarChangeListener(seek_lis);
        
        ratingBar1 = (RatingBar)findViewById(R.id.rating_bar);
        ratingBar1.setMax(100);   
        ratingBar1.setNumStars(5);
        ratingBar1.setOnRatingBarChangeListener(rat_lis);
    }
    
    private OnSeekBarChangeListener seek_lis = new OnSeekBarChangeListener() {
		
		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			Log.i("time",""+seekBar);
		}
		
		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			Log.i("time",""+seekBar);
		}
		
		/***
		 * 第三个参数是,有人工手动去拖动还是代码的拖动
		 */
		@Override
		public void onProgressChanged(SeekBar seekBar, int arg1, boolean arg2) {
			Log.i("time",""+seekBar+"-->"+arg1+"-->"+arg2);
		}
	};
	
	private OnRatingBarChangeListener rat_lis = new OnRatingBarChangeListener() {
		
		@Override
		public void onRatingChanged(RatingBar ratingBar, float press, boolean fromUser) {
			Log.i("time", ratingBar+"-->"+press+"-->"+fromUser);
		}
	};
    
	
    private OnClickListener onclickLis = new OnClickListener() {
		
		@Override
		public void onClick(View view) {
			if(view.getId()==R.id.prs_button1){
				progressBar1.incrementProgressBy(10);
			}else if(view.getId()==R.id.prs_button2){
				progressBar1.incrementSecondaryProgressBy(10);
			}
		}
	};
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}


二、在标题中使用进度条

1、对应的测试XML文件

<?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" >
    
    
    <Button
        android:id="@+id/barbut1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/showBtn"
        />
    
     <Button
        android:id="@+id/barbut2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/hiddenBtn"
        />
       

</LinearLayout>

2、对应的java代码

package com.example.test_list_view;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;

public class TestWindowBar extends Activity{
	
	private Button btn1,btn2;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//启用窗口中显示进度的进度条
		requestWindowFeature(Window.FEATURE_PROGRESS);
		//启用窗口不显示进度的进度条
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setContentView(R.layout.test_window_bar);
		btn1 = (Button)findViewById(R.id.barbut1);
		btn2 = (Button)findViewById(R.id.barbut2);
		btn1.setOnClickListener(onClickListener);
		btn2.setOnClickListener(onClickListener);
	}
	
	private OnClickListener onClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View view) {
			if(view.getId()==R.id.barbut1){
				setProgressBarIndeterminateVisibility(true);
				setProgressBarVisibility(true);
				setProgress(100);
			}
			if(view.getId()==R.id.barbut2){
				setProgressBarIndeterminateVisibility(false);
				setProgressBarVisibility(false);
			}
		}
	};

}



 类似资料: