安卓 toast, handler, progressBar, Button实例
2012-02-26 08:15
232 查看
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"); 4000 } //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>
相关文章推荐
- Toast和Progressbar结合的实例
- android Handler控制进度条(ProgressBar)实例
- handler实例,progressbar
- Android深入浅出系列之实例应用—程序运行进度提示条应用ProgressBar和Handler的使用
- Android深入浅出系列之实例应用—程序运行进度提示条应用ProgressBar和Handler的使用
- 安卓--信息提示框(Toast)实例
- Android深入浅出系列之实例应用—程序运行进度提示条应用ProgressBar和Handler的使用
- Toast和Handler的间隔使用实例
- 安卓018 Dialog & Toast & Notification & Menu
- Android --- Handler 多线程通信示例(ProgressBar)
- 【安卓】使用Handler完成子线程更新ui
- Message,MessageQueue,Looper,Handler详解+实例
- Eclipse做安卓的时候,button按钮把那个textview文本挡住了
- 安卓初学者必看实例,(计算圆面积)
- python 爬虫(3) Opener与Handler的介绍和实例应用
- 安卓上传图片实例,multipart/form-data实现安卓上传文件的功能
- hybird app项目实例:安卓webview中HTML5拍照图片上传
- Android 中Message,MessageQueue,Looper,Handler详解+实例
- Android 中Message,MessageQueue,Looper,Handler详解+实例
- 鱼鱼Chen之学写自己的apk(五)使用Handler完成Android里的多线程操作实例