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; } }

 

转载于:https://www.cnblogs.com/zn615/p/8202730.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值