Android studio控件之ProgressBar

ProgressBar用于显示进度条,表示我们的程序正在加载数据
activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<Button
    android:id="@+id/but"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="点击"/>
    <ProgressBar
        android:id="@+id/ProgressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

MainActivity.java:

package com.example.boring2;

import android.app.ProgressDialog;
import android.content.Intent;
import android.nfc.Tag;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Button button=(Button)findViewById(R.id.but);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ProgressBar progressBar=(ProgressBar)findViewById(R.id.ProgressBar1);//获取控件
                if(progressBar.getVisibility()==View.GONE){
                    progressBar.setVisibility(View.VISIBLE);
                }
                else{
                    progressBar.setVisibility(View.GONE);//setVisibility设置控件可见性
                    //View.GONE不可见 View.VISIBLE可见 View.INVISIBLE透明性质,不可见在还占空间
                }
            }
        });
    }
}

在这里插入图片描述点击一下按钮
在这里插入图片描述
除此之外,还可以改变ProgressBar形状
只需要加这两句代码即可改变形状
在这里插入图片描述
效果为:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
效果你一直多次点击按钮就知道了
在这里插入图片描述

发布了29 篇原创文章 · 获赞 16 · 访问量 1万+
展开阅读全文

使用安卓progressBar进度条实现秒表计时

06-14

如题,点击即使按钮后线程启动,但是界面上的TextView与PorgressBar并不显示 package com.example.progressbartime; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v4.app.Fragment; import android.support.v7.app.ActionBarActivity; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.ProgressBar; import android.widget.TextView; public class MainActivity extends ActionBarActivity { private Button start,stop; private ProgressBar hbar,mbar,sbar,csbar; private TextView htv,mtv,stv,cstv; private Thread mThread; private Handler mHandler; private int hour=0,minute=0,second=0,cssecond=0; private int STOP=0; private final static int MSG=1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); hbar=(ProgressBar) findViewById(R.id.hourBar); sbar=(ProgressBar) findViewById(R.id.secondBar); mbar=(ProgressBar) findViewById(R.id.minuteBar); csbar=(ProgressBar) findViewById(R.id.CSecondBar); htv=(TextView) findViewById(R.id.hourText); mtv=(TextView) findViewById(R.id.minuteTV); stv=(TextView) findViewById(R.id.secondTV); cstv=(TextView) findViewById(R.id.CSecondTV); start=(Button) findViewById(R.id.startBtn); stop=(Button) findViewById(R.id.stopBtn); start.setOnClickListener(new startBtnClick()); stop.setOnClickListener(new stopBtnClick()); //设置进度条的最大值以及当前进度 hbar.setMax(1000); //hbar.setProgress(0); mbar.setMax(60); //mbar.setProgress(0); sbar.setMax(60); //sbar.setProgress(0); csbar.setMax(100); //csbar.setProgress(0); mThread=new mThread(); mHandler=new mHandler(); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()) .commit(); } } public class startBtnClick implements OnClickListener { @Override public void onClick(View arg0) { STOP=1; mThread.start(); } } public class stopBtnClick implements OnClickListener { @Override public void onClick(View arg0) { STOP=0; } } public class mHandler extends Handler { @Override public void handleMessage(Message msg) { switch(msg.what){ case MSG:{ int csecond=((Integer) msg.obj)%101; if(csecond==100){ second++; csecond=0; } if(second==60){ minute++; second=0; } if(minute==60){ hour++; minute=0; } hbar.setProgress(hour); mbar.setProgress(minute); sbar.setProgress(second); csbar.setProgress(csecond); String h=Integer.toString(hour)+"h"; String m=Integer.toString(minute)+"m"; String s=Integer.toString(second)+"s"; String mm=Integer.toString(csecond)+"mm"; htv.setText(h); mtv.setText(m); stv.setText(s); cstv.setText(mm); break; } } } } public class mThread extends Thread { @Override public void run() { while(STOP==1){ try { Thread.sleep(10); cssecond++; Message message=new Message(); mHandler.obtainMessage(MSG,cssecond).sendToTarget(); } catch (InterruptedException e) { e.printStackTrace(); } } } } @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; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } /** * A placeholder fragment containing a simple view. */ public static class PlaceholderFragment extends Fragment { public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); return rootView; } } } 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览