I have some questions about android ui api.
Give a example, that I want to implement.
Main_UI_开发者_开发技巧Thread.java :
public class Main_UI_Thread extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*** do something about layout ***/
...
DisplayClass dc = new DisplayClass();
Thread th = new Thread(dc);
th.start();
}
}
DisplayClass.java :
public class DisplayClass extends Thread{
@Override
public void run() {
if( something happen ) {
to display Dialog or Toast show
and handle the ui listener
}
}
}
I know the message passing can be do that;
But I want the ui program is be implemented in DisplayClass.java
Is it possible??
My English is not well.^^"
Thanks everybody to give me some suggestions. :P
Your DisplayClass
would probably need a reference to your Activity
instance, and modify the UI by calling the Activity
's methods (or UI modifying methods that accept the Activity
instance as an argument).
Also, remember to always use the runOnUiThread
method to perform UI modifying actions from your "own" threads.
No need for DisplayClass to extend Thread
if you are doing new Thread(dc);
. Implementing Runnable is good enough.
public class DisplayClass extends Runnable {
private Activity activity;
public DisplayClass(Activity activity) {
this.activity = activity;
}
@Override
public void run() {
if (something happen) {
activity.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(activity, "Hello", Toast.LENGTH_SHORT).show();
}
});
}
}
}
精彩评论