Android中button的onClick事件几种方法
Android中button的onClick事件几种方法
成都网站建设哪家好,找成都创新互联!专注于网页设计、重庆网站建设公司、微信开发、小程序开发、集团企业网站建设等服务项目。核心团队均拥有互联网行业多年经验,服务众多知名企业客户;涵盖的客户类型包括:塑料袋等众多领域,积累了大量丰富的经验,同时也获得了客户的一致赞扬!
利用三种方法,学习button的监听事件。
方法一源码如下:
package com.example.androidtest; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import android.view.View; import android.widget.TextView; import android.view.View.OnClickListener; import android.widget.Button; //import android.text.Editable; //import android.text.TextWatcher; import android.widget.EditText; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button1 = (Button)findViewById(R.id.button1); Button button2 = (Button)findViewById(R.id.button2); //方法一: //增加事件响应 button1.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { Intent intent =new Intent(); intent.setClass(MainActivity.this, TestActivity.class); MainActivity.this.startActivity(intent); } }); button2.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { Intent intent =new Intent(); intent.setClass(MainActivity.this, TActivity.class); MainActivity.this.startActivity(intent); } }); }
方法二:
package com.example.androidtest; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import android.view.View; import android.widget.TextView; import android.view.View.OnClickListener; import android.widget.Button; //import android.text.Editable; //import android.text.TextWatcher; import android.widget.EditText; public class MainActivity extends Activity { @Override { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button1 = (Button)findViewById(R.id.button1); Button button2 = (Button)findViewById(R.id.button2); } //方法二: public void myClickHandler(View target){ switch (target.getId()) { case R.id.button1: Intent music=new Intent(); music.setClass(MainActivity.this, TestActivity.class); MainActivity.this.startActivity(music); // setTitle("myClickHandler01"); break; case R.id.button2: // setTitle("myClickHandler02"); Intent moive=new Intent(); moive.setClass(MainActivity.this, TActivity.class); MainActivity.this.startActivity(moive); break; } } }
方法三:
package com.example.androidtest; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import android.view.View; import android.widget.TextView; import android.view.View.OnClickListener; import android.widget.Button; //import android.text.Editable; //import android.text.TextWatcher; import android.widget.EditText; public class MainActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //方法三: public void myClickHandler01(View target){ //设置activity的头标题 // setTitle("myClickHandler01"); Intent music=new Intent(); music.setClass(MainActivity.this, TestActivity.class); MainActivity.this.startActivity(music); } public void myClickHandler02(View target){ //设置activity的头标题 // setTitle("myClickHandler02"); Intent moive=new Intent(); moive.setClass(MainActivity.this, TActivity.class); MainActivity.this.startActivity(moive); } }
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
新闻名称:Android中button的onClick事件几种方法
文章转载:http://cdiso.cn/article/gjsece.html