标签:image break rri 实例 data- bre data tco idg
5、自己定义方法,配置Android:onclick属性
import android.media.JetPlayer.OnJetEventListener; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.app.Dialog; import android.content.DialogInterface; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageButton; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener{ Button button1,button2,button4,button5; ImageButton button3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1 = (Button)findViewById(R.id.button1); button1.setOnClickListener(new OnClickListener() { //第一种方式:匿名内部内实现的按钮点击事件 @Override public void onClick(View arg0) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "匿名内部内实现的按钮点击事件", Toast.LENGTH_SHORT).show(); } }); button2 = (Button)findViewById(R.id.button2); button2.setOnClickListener(this); //第三种:创建实例化接口对象 button3 = (ImageButton)findViewById(R.id.button3); OnClickListener listener = new OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "创建实例化接口对象", Toast.LENGTH_SHORT).show(); } }; button3.setOnClickListener(listener); //第四种方式:使用内部类 button4 = (Button)findViewById(R.id.button4); button4.setOnClickListener(new MyOnClickListener()); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } //另外一种方式:实现View.onClickListener接口实现接口中的onClick()方法 @Override public void onClick(View arg0) { // TODO Auto-generated method stub switch(arg0.getId()){ case R.id.button2: Toast.makeText(this, "实现View.onClickListener接口实现接口中的onClick()方法",Toast.LENGTH_SHORT).show(); break; } } class MyOnClickListener implements OnClickListener{ @Override public void onClick(View arg0) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "内部类的方式", Toast.LENGTH_SHORT).show(); } } //第五种方式:自己定义方法:配置android:onClick属性 public void onButton(View view){ switch(view.getId()){ case R.id.button5: Toast.makeText(this, "自己定义方法,配置android:onClick属性", Toast.LENGTH_SHORT).show(); break; } } public void onButton7(View view){ Toast.makeText(this, "aaaaaaaaaaaaaaaaaaaaa", Toast.LENGTH_SHORT).show(); setContentView(R.layout.activity_main2); } }
标签:image break rri 实例 data- bre data tco idg
原文地址:http://www.cnblogs.com/yutingliuyl/p/6940492.html