ProgressBar
Layout部分:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".MainActivity"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:layout_width="80dp"
android:textColor="#FF0000"
android:id="@+id/tv_main_desc"
android:textSize="30dp"
android:layout_height="match_parent" />
<ProgressBar
android:layout_width="match_parent"
style="?android:attr/progressBarStyleHorizontal"
android:id="@+id/pb_main_download"
android:max="100"
android:layout_height="match_parent" />
</FrameLayout>
<Button
android:layout_width="match_parent"
android:text="启动下载"
android:onClick="doClick"
android:layout_height="wrap_content" />
</LinearLayout>
Java部分:
import android.os.Handler;
import android.os.Message;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private int p=0;//当前进度
private ProgressBar pb;//进度条
private TextView tv;//显示文本的控件
private MyHandler myHandler=new MyHandler();//新写的Handler类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件对象并与控件进行绑定
pb=findViewById(R.id.pb_main_download);
tv=findViewById(R.id.tv_main_desc);
}
public class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int code=msg.what;//接受处理码
switch (code){//这里使用switch便于扩充其它消息
case 1:
p++;//计数器累加
pb.setProgress(p);//给进度条的当前进度赋值
tv.setText(p+"%");//显示当前进度为多少
break;
}
}
}
//定义一个线程类继承自Thread类
public class myThread extends Thread{
@Override
public void run() {//重写run方法
super.run();
while(true){
try {
Thread.sleep(100);//使线程休眠0.1秒
} catch (InterruptedException e) {
e.printStackTrace();
}
if(p==100){//当前进度等于总进度时退出循环
p=0;
break;
}
Message msg=new Message();//创建消息对象
msg.what=1;
myHandler.sendMessage(msg);//发送处理码
}
}
}
public void doClick(View v){
if(0==p){//如果当前进度为0
new myThread().start();//开启线程
}
}
}
SeekBar
Layout部分:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".MainActivity"
android:orientation="vertical">
<SeekBar
android:id="@+id/sb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxHeight="5.0dp"
android:minHeight="5.0dp"/>
<TextView
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="拖动试试"/>
</LinearLayout>
Java部分:
import android.content.Context;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private SeekBar sb;//定义拖动控件对象
private TextView txt;//定义文本控件对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件对象并与之绑定
sb = findViewById(R.id.sb);
txt = findViewById(R.id.txt);
//设置拖动控件改变监听事件
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
txt.setText("当前进度值:" + progress + " / 100 ");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Toast.makeText(MainActivity.this, "触碰SeekBar", Toast.LENGTH_SHORT).show();
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
Toast.makeText(MainActivity.this, "放开SeekBar", Toast.LENGTH_SHORT).show();
}
});
}
}
RatingBar
Layout部分:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".MainActivity">
<RatingBar
android:id="@+id/rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:rating="1.5"/>
</LinearLayout>
Java部分:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private RatingBar rb;//定义评分控件对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rb = (RatingBar) findViewById(R.id.rb);//初始化评分控件
//设置评分控件改变监听事件
rb.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
//当评分发生改变时做出提示
Toast.makeText(MainActivity.this, "rating:" + String.valueOf(rating),
Toast.LENGTH_LONG).show();
}
});
}
}