zoukankan      html  css  js  c++  java
  • Android开发 ---基本UI组件4:拖动事件、评分进度条、圆圈式进度条、进度条控制

    Android开发 ---基本UI组件4

    1、activity_main.xml 

      描述:

        定义了一个按钮 

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/activity_main"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="其他控件"
            android:onClick="test_4"/>
    </LinearLayout>

    2、MainActivity.java

      描述:

        页面跳转

    package com.example.android_ui;
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }public void test_4(View view){
            Intent intent=new Intent(this,OtherUIActivity.class);
            startActivity(intent);
        }
    }

    3、activity_other_ui.xml

      描述:

        1、一段文本

        2、一个进度条 --SeekBar

        3、一个 星星评分条

        4、一个画圆圈式进度条

        5、从系统Theme.xml中引入的资源设置一个进度条 -----?表示从Theme中查找引用的资源名   

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:id="@+id/activity_other_ui"
        android:layout_width="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/showText"
            android:text="我是一段文字"
            android:textSize="25sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <SeekBar
            android:id="@+id/seekBar"
            android:max="60"
            android:progress="0"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <RatingBar
            android:id="@+id/ratingBar"
            android:max="5"
            android:progress="0"
            android:stepSize="0.5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
        <ProgressBar
            android:id="@+id/pb1"
            android:max="100"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <ProgressBar
            android:id="@+id/pb2"
            android:max="100"
            style="?android:progressBarStyleLarge"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <ProgressBar
            android:id="@+id/pb3"
            android:max="100"
            android:progress="10"
            style="?android:progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="horizontal"
            android:layout_height="wrap_content">
            <Button
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:text="开始"
                android:onClick="doStart"
                android:layout_height="wrap_content" />
            <Button
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:text="停止"
                android:onClick="doStop"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </LinearLayout>

    4、OtherUiActivity.java

    package com.example.android_ui;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.SystemClock;
    import android.view.View;
    import android.widget.ProgressBar;
    import android.widget.RatingBar;
    import android.widget.SeekBar;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class OtherUIActivity extends Activity {
      //获取进度条
        private SeekBar seekBar;
      //获取文本框
    private TextView showText;
      //获取评分条
    private RatingBar ratingBar;
      //获取圆圈式进度条
    private ProgressBar pb3;
      
    int count=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_other_ui);
    showText
    =(TextView)findViewById(R.id.showText); seekBar=(SeekBar)findViewById(R.id.seekBar); ratingBar=(RatingBar)findViewById(R.id.ratingBar); pb3=(ProgressBar)findViewById(R.id.pb3);
    //seekBar拖动事件
         /*

          拖动进度条的事件监听需要实现SeekBar.OnSeekBarChangeListener接口,

          调用SeekBar的setOnSeekBarChangeListener把该事件监听对象传递进去进行事件监听。

          事件监听接口中有三个重要的方法:

            1、onStartTrackingTouch方法

            该方法拖动进度条开始拖动的时候调用。

            2、onStopTrackingTouch方法

            该方法拖动进度条停止拖动的时候调用

            3、onProgressChanged

            该方法拖动进度条进度改变的时候调用

          */

            seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
           //参数b用来判断拖动是否结束,参数i表示当前进度
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
             //seekBar.getProgress()是获取拖动条当前值
             //shoqText.setTextSize()自绘制文字的大小
             //实现通过得到拖动条当前值来改变字体大小的目的 showText.setTextSize(seekBar.getProgress()); } @Override
    public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { //Toast.makeText(OtherUIActivity.this,"sk:"+seekBar.getProgress(),Toast.LENGTH_LONG).show(); } }); //使用线程控制seekBar一直变化 new Thread(new Runnable() { @Override public void run() { while(count<60){ //操作UI的代码运行在UI线程中 runOnUiThread(new Runnable() { @Override public void run() {
                     //设置进度条的当前值 seekBar.setProgress(count); } }); count
    ++;
                //每改变一次停留半秒 SystemClock.sleep(
    500); } } }).start();      //给星星评分条设置改变监听事件 ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() { @Override public void onRatingChanged(RatingBar ratingBar, float v, boolean b) { Toast.makeText(OtherUIActivity.this,"你打了"+v+"分",Toast.LENGTH_SHORT).show(); } }); }   
      
      //设置点击开始按钮,进度条开始运行,
    boolean isRun=true;
      //进度条从0开始
    int down=0; public void doStart(View view){ new Thread(new Runnable() { @Override public void run() { while(isRun){ //更新下载进度 down++; pb3.setProgress(down);//唯一一个可以在UI多线程中更新的 SystemClock.sleep(200); if(down>=100){ runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(OtherUIActivity.this,"数据下载完毕",Toast.LENGTH_LONG).show(); //当数据下载完毕时调用doStop()方法,停止进度条运行
                       doStop(
    null); } }); } } } }).start(); }   //当点击停止按钮时,进度条停止运行,并将进度条当前位置的值清空 public void doStop(View view){ isRun=false; pb3.setProgress(0); down=0; } }
    如果您发现博客内容有什么错误,请您下方留言
  • 相关阅读:
    对象数组化 Object.values(this.totalValueObj).forEach(value => {
    禅道
    Ant Design Ant Design 实战教程(beta 版) 在 model 中请求服务端数据
    react 环境搭建
    返回运行方法,可以写在一行 callback&&callback()
    computed 里面 不能写箭头函数 都要写 function () {} 否则页面会都不显示
    webStorm -> Version Control _> Repository -> Filter By User 查看svn日志
    created:异步初始化数据都应该放到 created里面
    收藏夹
    keep-alive 必须 页面有name 要不缓存不住数据
  • 原文地址:https://www.cnblogs.com/zn615/p/8202730.html
Copyright © 2011-2022 走看看