android 怎么在progressbar中添加文字
答案:1 悬赏:0 手机版
解决时间 2021-04-15 04:54
- 提问者网友:刺鸟
- 2021-04-14 14:29
android 怎么在progressbar中添加文字
最佳答案
- 五星知识达人网友:十年萤火照君眠
- 2021-04-14 15:39
在progressbar中添加文字
01 public class MyProgress extends ProgressBar{
02 String text;
03 Paint mPaint;
04
05 public MyProgress(Context context) {
06 super(context);
07 // TODO Auto-generated constructor stub
08 System.out.println("1");
09 initText();
10 }
11
12 public MyProgress(Context context, AttributeSet attrs, int defStyle) {
13 super(context, attrs, defStyle);
14 // TODO Auto-generated constructor stub
15 System.out.println("2");
16 initText();
17 }
18
19
20 public MyProgress(Context context, AttributeSet attrs) {
21 super(context, attrs);
22 // TODO Auto-generated constructor stub
23 System.out.println("3");
24 initText();
25 }
26
27 @Override
28 public synchronized void setProgress(int progress) {
29 // TODO Auto-generated method stub
30 setText(progress);
31 super.setProgress(progress);
32
33 }
34
35 @Override
36 protected synchronized void onDraw(Canvas canvas) {
37 // TODO Auto-generated method stub
38 super.onDraw(canvas);
39 //this.setText();
40 Rect rect = new Rect();
41 this.mPaint.getTextBounds(this.text, 0, this.text.length(), rect);
42 int x = (getWidth() / 2) - rect.centerX();
43 int y = (getHeight() / 2) - rect.centerY();
44 canvas.drawText(this.text, x, y, this.mPaint);
45 }
46
47 //初始化,画笔
48 private void initText(){
49 this.mPaint = new Paint();
50 this.mPaint.setColor(Color.WHITE);
51
52 }
53
54 private void setText(){
55 setText(this.getProgress());
56 }
57
58 //设置文字内容
59 private void setText(int progress){
60 int i = (progress * 100)/this.getMax();
61 this.text = String.valueOf(i) + "%";
62 }
63
64
65 }
这样一个可以满足我们基本需求的进度条就写好了。
用的时候就可以直接在layout的XML里面加了,不过添加的写法稍微有点不同。标记名要写成这个自定义进度条的完整类名,就像下面这样。
1 2 android:id="@+id/pgsBar"
3 android:max="100"
4 android:layout_width="fill_parent"
5 android:layout_height="wrap_content"
6
7 android:visibility="visible"
8 />
这样写后,可能会因为命名空间改变,下面属性无法用代码提示。一个简单的做法就是,先写一个正常的ProgressBar的标记,把属性写完后,再将ProgressBar替换为我们自定义的进度条的完整类名。
最后,使用方法就和普通的ProgressBar差不多了。
01 public class ProgressTest extends Activity {
02
03 private Button btn_go = null;
04 private MyProgress myProgress = null;
05 private Handler mHandler;
06
07
08 @Override
09 public void onCreate(Bundle savedInstanceState) {
10 super.onCreate(savedInstanceState);
11 setContentView(R.layout.main);
12 findView();
13 setParam();
14 addListener();
15
16 mHandler = new Handler(new Callback() {
17
18 @Override
19 public boolean handleMessage(Message msg) {
20 // TODO Auto-generated method stub
21 myProgress.setProgress(msg.what);
22 return false;
23 }
24 });
25
26 }
27
28 private void findView(){
29 btn_go = (Button) findViewById(R.id.btn_go);
30 myProgress = (MyProgress) findViewById(R.id.pgsBar);
31 }
32
33 private void setParam(){
34 btn_go.setText("开始");
35 }
36 private void addListener(){
37 btn_go.setOnClickListener(new OnClickListener() {
38
39 @Override
40 public void onClick(View v) {
41 // TODO Auto-generated method stub
42 new Thread(new Runnable() {
43 @Override
44 public void run() {
45 // TODO Auto-generated method stub
46 for(int i = 0;i <=50; i++){
47 mHandler.sendEmptyMessage(i * 2);
48 try {
49 Thread.sleep(80);
50 } catch (InterruptedException e) {
51 // TODO Auto-generated catch block
52 e.printStackTrace();
53 }
54 }
55 }
56 }).start();
57 }
58 });
59 }
60 }
01 public class MyProgress extends ProgressBar{
02 String text;
03 Paint mPaint;
04
05 public MyProgress(Context context) {
06 super(context);
07 // TODO Auto-generated constructor stub
08 System.out.println("1");
09 initText();
10 }
11
12 public MyProgress(Context context, AttributeSet attrs, int defStyle) {
13 super(context, attrs, defStyle);
14 // TODO Auto-generated constructor stub
15 System.out.println("2");
16 initText();
17 }
18
19
20 public MyProgress(Context context, AttributeSet attrs) {
21 super(context, attrs);
22 // TODO Auto-generated constructor stub
23 System.out.println("3");
24 initText();
25 }
26
27 @Override
28 public synchronized void setProgress(int progress) {
29 // TODO Auto-generated method stub
30 setText(progress);
31 super.setProgress(progress);
32
33 }
34
35 @Override
36 protected synchronized void onDraw(Canvas canvas) {
37 // TODO Auto-generated method stub
38 super.onDraw(canvas);
39 //this.setText();
40 Rect rect = new Rect();
41 this.mPaint.getTextBounds(this.text, 0, this.text.length(), rect);
42 int x = (getWidth() / 2) - rect.centerX();
43 int y = (getHeight() / 2) - rect.centerY();
44 canvas.drawText(this.text, x, y, this.mPaint);
45 }
46
47 //初始化,画笔
48 private void initText(){
49 this.mPaint = new Paint();
50 this.mPaint.setColor(Color.WHITE);
51
52 }
53
54 private void setText(){
55 setText(this.getProgress());
56 }
57
58 //设置文字内容
59 private void setText(int progress){
60 int i = (progress * 100)/this.getMax();
61 this.text = String.valueOf(i) + "%";
62 }
63
64
65 }
这样一个可以满足我们基本需求的进度条就写好了。
用的时候就可以直接在layout的XML里面加了,不过添加的写法稍微有点不同。标记名要写成这个自定义进度条的完整类名,就像下面这样。
1
3 android:max="100"
4 android:layout_width="fill_parent"
5 android:layout_height="wrap_content"
6
7 android:visibility="visible"
8 />
这样写后,可能会因为命名空间改变,下面属性无法用代码提示。一个简单的做法就是,先写一个正常的ProgressBar的标记,把属性写完后,再将ProgressBar替换为我们自定义的进度条的完整类名。
最后,使用方法就和普通的ProgressBar差不多了。
01 public class ProgressTest extends Activity {
02
03 private Button btn_go = null;
04 private MyProgress myProgress = null;
05 private Handler mHandler;
06
07
08 @Override
09 public void onCreate(Bundle savedInstanceState) {
10 super.onCreate(savedInstanceState);
11 setContentView(R.layout.main);
12 findView();
13 setParam();
14 addListener();
15
16 mHandler = new Handler(new Callback() {
17
18 @Override
19 public boolean handleMessage(Message msg) {
20 // TODO Auto-generated method stub
21 myProgress.setProgress(msg.what);
22 return false;
23 }
24 });
25
26 }
27
28 private void findView(){
29 btn_go = (Button) findViewById(R.id.btn_go);
30 myProgress = (MyProgress) findViewById(R.id.pgsBar);
31 }
32
33 private void setParam(){
34 btn_go.setText("开始");
35 }
36 private void addListener(){
37 btn_go.setOnClickListener(new OnClickListener() {
38
39 @Override
40 public void onClick(View v) {
41 // TODO Auto-generated method stub
42 new Thread(new Runnable() {
43 @Override
44 public void run() {
45 // TODO Auto-generated method stub
46 for(int i = 0;i <=50; i++){
47 mHandler.sendEmptyMessage(i * 2);
48 try {
49 Thread.sleep(80);
50 } catch (InterruptedException e) {
51 // TODO Auto-generated catch block
52 e.printStackTrace();
53 }
54 }
55 }
56 }).start();
57 }
58 });
59 }
60 }
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯