单击按钮事件
1 import android.os.Bundle; 2 import android.app.Activity; 3 import android.view.Menu; 4 import android.view.View; 5 import android.widget.Button; 6 import android.widget.Toast; 7 8 public class MainActivity extends Activity { 9 10 @Override 11 protected void onCreate(Bundle savedInstanceState) { 12 super.onCreate(savedInstanceState); 13 setContentView(R.layout.activity_main); 14 15 //获得按钮ID 16 Button button = (Button)findViewById(R.id.MyButtonClick); 17 //绑定按钮监听器触发事件 18 button.setOnClickListener(new Button.OnClickListener(){ 19 20 @Override 21 public void onClick(View v) { 22 // TODO Auto-generated method stub 23 Toast.makeText(MainActivity.this, "iu", Toast.LENGTH_LONG).show(); 24 } 25 26 }); 27 } 28 29 @Override 30 public boolean onCreateOptionsMenu(Menu menu) { 31 // Inflate the menu; this adds items to the action bar if it is present. 32 getMenuInflater().inflate(R.menu.main, menu); 33 return true; 34 } 35 36 } 37