标签:
Android handle
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context=".MainActivity" > 10 11 <TextView 12 android:id="@+id/text_id" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:text="@string/hello_world" /> 16 <Button 17 android:id="@+id/btn" 18 android:layout_width="wrap_content" 19 android:layout_height="wrap_content" 20 android:text="Button" 21 android:layout_below="@id/text_id" 22 23 /> 24 25 </RelativeLayout>
1 package com.ibox365.s0725001; 2 3 import android.os.Bundle; 4 import android.os.Handler; 5 import android.os.Message; 6 import android.app.Activity; 7 import android.view.Menu; 8 import android.view.View; 9 import android.view.View.OnClickListener; 10 import android.widget.Button; 11 12 public class MainActivity extends Activity { 13 14 private Button button; 15 private Handler handler; 16 17 @Override 18 protected void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_main); 21 22 button=(Button) findViewById(R.id.btn); 23 button.setOnClickListener(new ButtonListen()); 24 25 handler=new myHandle(); 26 27 } 28 29 class ButtonListen implements OnClickListener 30 { 31 32 /* (non-Javadoc) 33 * @see android.view.View.OnClickListener#onClick(android.view.View) 34 */ 35 @Override 36 public void onClick(View v) { 37 // TODO Auto-generated method stub 38 39 Message msg=handler.obtainMessage(); 40 msg.what=2; 41 handler.sendMessage(msg); 42 } 43 44 45 } 46 47 class myHandle extends Handler{ 48 49 /* (non-Javadoc) 50 * @see android.os.Handler#handleMessage(android.os.Message) 51 */ 52 @Override 53 public void handleMessage(Message msg) { 54 // TODO Auto-generated method stub 55 super.handleMessage(msg); 56 57 int what =msg.what; 58 System.out.println(what); 59 60 } 61 62 63 } 64 65 @Override 66 public boolean onCreateOptionsMenu(Menu menu) { 67 // Inflate the menu; this adds items to the action bar if it is present. 68 getMenuInflater().inflate(R.menu.main, menu); 69 return true; 70 } 71 72 }
标签:
原文地址:http://www.cnblogs.com/laopo/p/5703057.html