Runnable runnable = new Runnable() { public void run() { handler.post(new Runnable() { public void run() { // Handler thread // Call method() } }); } }; new Thread(runnable).start();
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="39dp" > <RadioButton android:id="@+id/rdo1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="Red" /> <RadioButton android:id="@+id/rdo2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Blue" /> <RadioButton android:id="@+id/rdo3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Green" /> </RadioGroup> <Button android:id="@+id/button1" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/radioGroup1" android:layout_centerHorizontal="true" android:layout_marginTop="15dp" android:text="Run Thread" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="10dp" android:text="Select Thread" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/txtResult" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/button1" android:layout_marginTop="14dp" android:text="Result" /> </RelativeLayout>
package com.myapp; import android.os.Bundle; import android.os.Handler; import android.app.Activity; import android.text.Html; import android.view.Menu; import android.view.View; import android.widget.RadioButton; import android.widget.TextView; import android.widget.Button; import android.graphics.*; public class MainActivity extends Activity { private Handler handler = new Handler(); RadioButton rdo1; RadioButton rdo2; RadioButton rdo3; TextView result; String resultOut = ""; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // rdo1 Red rdo1 = (RadioButton) findViewById(R.id.rdo1); rdo1.setTextColor(Color.RED); // rdo2 Blue rdo2 = (RadioButton) findViewById(R.id.rdo2); rdo2.setTextColor(Color.BLUE); // rdo3 Blue rdo3 = (RadioButton) findViewById(R.id.rdo3); rdo3.setTextColor(Color.GREEN); // txtResult result = (TextView) findViewById(R.id.txtResult); // button1 Button btn = (Button) findViewById(R.id.button1); btn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Perform action on click if (rdo1.isChecked()) { startProgress("RED"); } else if (rdo2.isChecked()) { startProgress("BLUE"); } else if (rdo3.isChecked()) { startProgress("GREEN"); } } }); } public void startProgress(final String color) { Runnable runnable = new Runnable() { public void run() { final String threadColor = color; for (int i = 0; i <= 10; i++) { final int value = i; try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } handler.post(new Runnable() { public void run() { // Handler thread String curThread = "<br><font color='" + threadColor.toString() + "'>Thread <b>" + threadColor.toString() + "</b>" + ": Process i=" + value + "</font>"; resultOut = resultOut + curThread; result.setText(Html.fromHtml(resultOut)); } }); } } }; new Thread(runnable).start(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
for (int i = 0; i <= 10; i++) { final int value = i; try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } handler.post(new Runnable() { public void run() { // Handler thread String curThread = "<br><font color='" + threadColor.toString() + "'>Thread <b>" + threadColor.toString() + "</b>" + ": Process i=" + value + "</font>"; resultOut = resultOut + curThread; result.setText(Html.fromHtml(resultOut)); } }); }
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท