Home > Mobile >  Android studio threads and succession, how do you use the Handler
Android studio threads and succession, how do you use the Handler

Time:03-17

Public class Enter0 extends the Activity {
Protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RequestWindowFeature (Window. FEATURE_NO_TITLE);
The setContentView (R.l ayout. Activity_enter0);
TextView tv1=(TextView) enclosing the findViewById (R.i which 1);
Cc suo=new cc ();
Thread t=new Thread (suo);
T.s tart ();
};
}
The class cc extends Enter0 implements Runnable {
Public void the run () {
Tv1. SetEnabled (false);
}
}

After using inheritance, in the back of the "cc" how to use "Enter0" the tv1!
Heard that is to use Handler, but Handler will not use ah, oneself see search on the net is cast, have good intention directly help me on this code change ah, very grateful

CodePudding user response:

Enter0. This. XXXX

CodePudding user response:

TextView tv1 into a member variable

CodePudding user response:

//the object can be passed as a parameter to cc class
Public class Enter0 extends the Activity {
Protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RequestWindowFeature (Window. FEATURE_NO_TITLE);
The setContentView (R.l ayout. Activity_enter0);
TextView tv1=(TextView) enclosing the findViewById (R.i which 1);
Cc suo=new cc (tv1);
Thread t=new Thread (suo);
T.s tart ();
};
}
The class cc extends Enter0 implements Runnable {
Private TextView tv1.

Public cc (TextView tv1) {
super();
Enclosing tv1=tv1;
}

Public void the run () {
Tv1. SetEnabled (false);
}
}

CodePudding user response:

Public class Main2Activity extends AppCompatActivity {

Private TextView TextView;

Private Handler Handler=new Handler () {
@ Override
Public void handleMessage (@ NonNull Message MSG) {
Super. HandleMessage (MSG);
The e (" TAG ", "handleMessage:");
The switch (MSG. What) {
Case 1:
String STR=(String) MSG. Obj;
Textview. SetText (STR);
break;
}
}
};

@ Override
Protected void onCreate (Bundle savedInstanceState) {
Super. OnCreate (savedInstanceState);
The setContentView (R.l ayout. Activity_main2);

Textview=the findViewById (R.i which extview);
Cc suo=new cc ();
Thread t=new Thread (suo);
T.s tart ();
}

The class cc extends Main2Activity implements Runnable {
Public void the run () {
The e (" TAG ", "run");
The Message the Message=new Message ();
message.what=1;
Message. Obj="test";
Handler. SendMessage (message);
}
}
}

To use the handler
  • Related