问题描述
- 使用安卓progressBar进度条实现秒表计时
-
如题,点击即使按钮后线程启动,但是界面上的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; } }
}
解决方案
hbar=(ProgressBar) findViewById(R.id.hourBar);
看看进度条找到没有
解决方案二:
hbar=(ProgressBar) findViewById(R.id.hourBar);
看看进度条找到没有
时间: 2024-11-18 13:08:39