安卓 toast, handler, progressBar, Button实例

myHandlerActivity.java
/*
 * 安卓 toast, handler, progressBar, Button实例
 * author: Derek Zhu 2012-2-26
 */
package com.derek.myHandler;

import android.app.Activity;
import android.opengl.Visibility;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class myHandlerActivity extends Activity {

	private Button startButton = null;
	private Button endButton = null;
	private Button resetBarButton = null;
	private Button speedUpButton = null;
	private Button speedDownButton = null;
	private TextView myTextView = null;
	private CheckBox toastCB = null;
	private CheckBox barCB = null;
	private CheckBox textViewCB = null;
	private ProgressBar bar = null;
	public int myThreadCounter = 0;
	public int myThreadCounter4Bar = 0;
	public int speed = 500;
	public boolean isToastCBChecked = false;
	public boolean isBarCBChecked = false;
	public boolean isTextViewCBChecked = false;
	

	// private CheckBox toastCB = (CheckBox) findViewById(R.id.toastCB);
	// private CheckBox barCB = (CheckBox) findViewById(R.id.barCB);
	// private CheckBox textViewCB = (CheckBox) findViewById(R.id.textCB);

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// findViewById
		bar = (ProgressBar) findViewById(R.id.bar);
		startButton = (Button) findViewById(R.id.startButton);
		endButton = (Button) findViewById(R.id.endButton);
		resetBarButton = (Button) findViewById(R.id.resetBar);
		speedUpButton = (Button) findViewById(R.id.speedUP);
		speedDownButton = (Button) findViewById(R.id.speedDown);
		myTextView = (TextView) findViewById(R.id.myTextView);
		bar = (ProgressBar) findViewById(R.id.bar);
		
		// the Buttons
		startButton.setOnClickListener(new startButtonListener());
		endButton.setOnClickListener(new endButtonListener());
		resetBarButton.setOnClickListener(new resetBarButtonListener());
		speedUpButton.setOnClickListener(new speedUpButtonListener());
		speedDownButton.setOnClickListener(new speedDownButtonListener());

		
		// the CheckBoxs:
		CheckBox toastCB = (CheckBox) findViewById(R.id.toastCB);
		CheckBox barCB = (CheckBox) findViewById(R.id.barCB);
		CheckBox textViewCB = (CheckBox) findViewById(R.id.textCB);

		toastCB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub

				if (isChecked){
					isToastCBChecked = true;
					System.out.println("toastCB is checked");
				} else {
					isToastCBChecked = false;
					System.out.println("toastCB is unchecked");
				}
			}
		});
		textViewCB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				
				if (isChecked){
					isTextViewCBChecked = true;
					myTextView.setText("you opened myTextView, i'm gonna have work to do here");
				} else {
					isTextViewCBChecked = false;
					myTextView.setText("myTextView was closed manually, yeah, we all know who did that");
				}
				//myTextView.
			}
		});
		barCB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				
				if (isChecked){
					bar.setVisibility(View.VISIBLE);
					isBarCBChecked = true;
					System.out.println("barCB is checked");
				} else {
					bar.setVisibility(View.INVISIBLE);
					isBarCBChecked = false;
					System.out.println("barCB is unchecked");
				}

			}
		}); 

		
		
		/*
		 * for (;;){ try { //设置当前显示睡眠1秒 Thread.sleep(1000); } catch
		 * (InterruptedException e) { // TODO Auto-generated catch block
		 * e.printStackTrace(); } if (toastCB.isChecked()){
		 * System.out.println("toastCB is Checked"); } else { continue; } }
		 */
	}
	
	// TODO button listener
	class startButtonListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			// reset my counter
			myThreadCounter = 0;
			handler.post(myThread);
		}
		
	}	
	class endButtonListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			handler.removeCallbacks(myThread);
			System.out.println("Okay, someone removed myThread, who it is!who it is!! Give me ur name!!!");
			
		}
		
	}
	class resetBarButtonListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO I kinda Thought this is also a stupid method to reset...
			myThreadCounter4Bar = myThreadCounter;
			bar.setProgress(0);
		}
		
	}
	
	class speedUpButtonListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			speed += 200;
		}
		
	}
	class speedDownButtonListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			speed -= 200;
		}
		
	}
	
	
	// handler
	Handler handler = new Handler();
	Runnable myThread = new Runnable(){

		@Override
		public void run() {
			// TODO Auto-generated method stub
			myThreadCounter += 1;
			System.out.println("This is the " + myThreadCounter + " Times my thread running");
			// mystuff
			/*
			if (toastCB.isChecked()){
				Toast.makeText(myHandlerActivity.this,"This is the thread running",Toast.LENGTH_SHORT); // " + myThreadCounter + " Times my 
			}
			*/
			// TODO there gotta be some way else other than stupid way like this...   3 boolen? god....
			System.out.println("ToastCB? " + isToastCBChecked + " TextViewCB? " + isTextViewCBChecked + " BarCB? " + isBarCBChecked);
			
			if (isToastCBChecked){
				Toast.makeText(myHandlerActivity.this,"This is the " + myThreadCounter + " Times my thread running",Toast.LENGTH_SHORT).show(); // 
			}
			if (isTextViewCBChecked){
				myTextView.setText("This is the " + myThreadCounter + " Times my thread running");
			}
			if (isBarCBChecked){
				bar.setProgress(myThreadCounter-myThreadCounter4Bar);
				System.out.println("New, the bar total Progress is: " + (int)(myThreadCounter-myThreadCounter4Bar));
			}
			// again
			handler.postDelayed(myThread, speed);
		}
		
	};
	
}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/myTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="myTextView was not open by default, well, don't blame your self for that" 
        android:cursorVisible="false"/>

	<ProgressBar
        android:id="@+id/bar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="invisible"
        android:max="100" />
	
	<CheckBox
        android:id="@+id/toastCB"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="toastCB" />
    <CheckBox
        android:id="@+id/textCB"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="textCB" />
    <CheckBox
        android:id="@+id/barCB"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="barCB" />
    <LinearLayout 
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:orientation="horizontal"
        >
	        
	    <Button
	        android:id="@+id/resetBar"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="resetBar" />
	    <Button
	        android:id="@+id/resetTextView"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="resetTextView" />
	    <Button
	        android:id="@+id/speedUP"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="speed +" />
	    <Button
	        android:id="@+id/speedDown"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="speed -" />
	        
    </LinearLayout>
    
    
    <Button
        android:id="@+id/startButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="start" />

    <Button
        android:id="@+id/endButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="end" />

    
</LinearLayout>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值