[Java] android handler →→→→→进入此内容的聊天室

来自 , 2019-10-15, 写在 Java, 查看 142 次.
URL http://www.code666.cn/view/1019c809
  1. package com.example.handlerdemo;
  2.  
  3. import java.util.HashMap;
  4.  
  5. import android.os.Bundle;
  6. import android.os.Handler;
  7. import android.os.Message;
  8. import android.app.Activity;
  9. import android.content.Intent;
  10. import android.util.Log;
  11. import android.view.Menu;
  12. import android.view.View;
  13. import android.view.View.OnClickListener;
  14. import android.widget.Button;
  15. import android.widget.EditText;
  16. import android.widget.RadioGroup;
  17. import android.widget.Spinner;
  18. import android.widget.TextView;
  19. import android.widget.Toast;
  20.  
  21. public class MainActivity extends Activity implements OnClickListener {
  22.  
  23.         private Button jia;
  24.         private Button jian;
  25.         private EditText numid1;
  26.         private EditText numid2;
  27.     private static TextView result ;  
  28.    
  29.     private String num1;
  30.         private String num2;
  31.        
  32.         private static String mresult;
  33.        
  34.         static final String NUM1 = "num1";
  35.         static final String NUM2 = "num2";
  36.        
  37.         //private thread mthread;
  38.        
  39.        
  40.         @Override
  41.         protected void onCreate(Bundle savedInstanceState) {
  42.                 super.onCreate(savedInstanceState);
  43.                 setContentView(R.layout.activity_main);
  44.                
  45.                 initview();
  46.                 initData();
  47.                 //mthread.start();
  48.                
  49.         }
  50.        
  51.        
  52.  
  53.         private void initview(){
  54.                
  55.                 numid1 = (EditText)this.findViewById(R.id.editText1);
  56.                 numid2 = (EditText)this.findViewById(R.id.editText2);//民族
  57.                
  58.                 result = (TextView)findViewById(R.id.textView1);
  59.                 jia = (Button)this.findViewById(R.id.button1);
  60.                 jian = (Button)this.findViewById(R.id.button2);
  61.                
  62.                 jia.setOnClickListener(this);
  63.                
  64.         }
  65.        
  66.         private void initData() {
  67.                 // TODO Auto-generated method stub
  68.                 thread mthread =new thread();
  69.                 mthread.start();
  70.                 num1 =numid1.getText().toString().trim();
  71.                 num2=numid2.getText().toString().trim();
  72.         }
  73.  
  74.        
  75.         public static Handler handler = new Handler() {
  76.                 public void handleMessage(Message msg) {
  77.                 super.handleMessage(msg);
  78.                 switch (msg.what) {
  79.                         case 0x123:
  80.                                
  81.                                 Integer temp = msg.arg1;
  82.                                 mresult = temp.toString();
  83.                                 result.setText(temp.toString());
  84.                                 //Toast.makeText(MainActivity.this,"请先打开Wifi或者3G网络", Toast.LENGTH_SHORT).show();       
  85.                                 break;
  86.                                
  87.                 }
  88.                
  89.                
  90.                 }
  91.                
  92.         };
  93.        
  94.         private void sendHandlerMsg(int what) {
  95.                 Message msg = new Message();
  96.                 msg.what = what;
  97.                 handler.sendMessage(msg);
  98.         }
  99.         private void sendHandlerMsg(int what, int timeout) {
  100.                 Message msg = new Message();
  101.                 msg.what = what;
  102.                 handler.sendMessageDelayed(msg, timeout);
  103.         }
  104.         private void sendHandlerMsg(Handler handler, int what, String obj) {
  105.                 Message msg = new Message();
  106.                 msg.what = what;
  107.                 msg.obj = obj;
  108.                 if (handler != null) {
  109.                         handler.sendMessage(msg);
  110.                 }
  111.         }
  112.        
  113.        
  114.         private void sendHandlerMsg(Handler handler,Message msg0) {
  115.                 Message msg = new Message();
  116.                 msg = msg0;
  117.                 //msg.obj = obj;
  118.                 if (handler != null) {
  119.                         handler.sendMessage(msg);
  120.                 }
  121.         }
  122.  
  123.         @Override
  124.         public void onClick(View v) {
  125.                 // TODO Auto-generated method stub
  126.                 // Toast.makeText(MainActivity.this,mresult, Toast.LENGTH_SHORT).show();
  127.                 /*thread mthread =new thread();
  128.                 mthread.start();*/
  129.                 switch (v.getId()) {
  130.                 case R.id.button1:
  131.                         initData();
  132.                         Message msg =new Message();
  133.                         msg.what = 0x123;
  134.                         Bundle bundle = new Bundle();
  135.                         bundle.putInt(NUM1, Integer.parseInt(num1));
  136.                         bundle.putInt(NUM2, Integer.parseInt(num2));
  137.                         msg.setData(bundle);
  138.                         sendHandlerMsg(thread.mhandler, msg);
  139.                         //sendHandlerMsg(thread.mhandler, 21, "654");
  140.                         break;
  141.                        
  142.                 case R.id.button2:
  143.                         Log.i("tag","number");
  144.                         break; 
  145.                        
  146.                 }
  147.                
  148.                
  149.         }
  150. @Override
  151.         public boolean onCreateOptionsMenu(Menu menu) {
  152.                 // Inflate the menu; this adds items to the action bar if it is present.
  153.                 getMenuInflater().inflate(R.menu.main, menu);
  154.                 return true;
  155.         }
  156.        
  157.         }
  158.  

回复 "android handler"

这儿你可以回复上面这条便签

captcha