android中ProgressBar的使用SeekBar的使用和RatingBar的使用

发布时间:2020-09-14 13:08:45 作者:xiaohongyangok
来源:网络 阅读:273

MainActivity.java文件代码

package com.example.android_pickers2;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
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;
import android.widget.Toast;
public class MainActivity extends Activity {
    Button button ;
    ProgressBar bar;
    SeekBar seekBar;
    RatingBar ratingBar;
             
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) this.findViewById(R.id.button1);
        bar = (ProgressBar) this.findViewById(R.id.progressBar1);
        bar.setMax(100);
        button.setOnClickListener(new OnClickListener() {
                     
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                MyTask task = new MyTask();
                task.execute();
            }
        });
        //SeekBar的使用
        seekBar = (SeekBar) this.findViewById(R.id.seekBar1);
        seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
                     
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
            }
                     
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
                         
            }
                     
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                Toast.makeText(MainActivity.this, "->>"+progress, 1).show();
                System.out.println("-->"+progress);
                // TODO Auto-generated method stub
                         
            }
        });
        //RatingBar的使用
        ratingBar = (RatingBar) this.findViewById(R.id.ratingBar1);
        ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
                     
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating,
                    boolean fromUser) {
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this, "->"+rating, 1).show();
                         
            }
        });
    }
             
    /*
     * 定义一个类继承AsyncTask类,动态改变ProgressBar
     */
    class MyTask extends AsyncTask<Void, Integer, Void>{
        @Override
        protected void onProgressUpdate(Integer... values) {
            // TODO Auto-generated method stub
            super.onProgressUpdate(values);
            //更新bar
            bar.setProgress(values[0]+20);
        }
                 
        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub
                     
            int i=1;
            while(i<=100){
                try {
                    Thread.sleep(100);
                } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                }
                publishProgress(i);
                i++;
            }
            return null;
        }
                 
    }
    @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;
    }
}


activity_main.xml代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="71dp"
        android:text="下载图片" />
    <ProgressBar
        android:id="@+id/progressBar1"
        
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true" />
    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/progressBar1"
        android:layout_marginTop="28dp" />
    <RatingBar
        android:id="@+id/ratingBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/seekBar1"
        android:layout_marginTop="30dp" />
</RelativeLayout>


推荐阅读:
  1. Android中RatingBar的自定义效果
  2. 怎么在Android中使用SeekBar改变图片的透明度

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android import gr

上一篇:CentOS7.2下安装docker容器教程

下一篇:关于MySQL自增ID的一些小问题总结

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》