在android中button响应的几种方式

 

1.

在布局文件中添加button的监听名字

Android:onClick=”buttonOnClick”

例如:

<Button

        android:id="@+id/button2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignRight="@+id/button1"

        android:layout_below="@+id/textView2"

        android:layout_marginTop="28dp"

        android:text="Button"

        android:onClick="buttonOnClick" />

 

在activity中创建响应的函数

publicvoid buttonOnClick(View view){

      Mytext1.setText("hello!");//设置文档的显示

   }

 

2.

在OnCreate函数中写

Mybutton = (Button)findViewById(R.id.button1);

Mybutton.setOnClickListener(new OnClickListener(){

         @Override

         publicvoid onClick(View v) {

         // TODO Auto-generated method stub

            Mytext.setText("hello!");//设置文档的显示

         }

         });

 

 

3.在OnCreate函数中写

 

findViewById(R.id.btn_title_popmenu).setOnClickListener(this);

findViewById(R.id.button1).setOnClickListener(this);

 

再来

publicvoid onClick(View v) {

      if(v.getId() == R.id.btn_title_popmenu){

         popMenu.showAsDropDown(v);

      }elseif(v.getId()==R.id.button1)

      {

         popMenu1.showAsDropDown(v);

      }

   }

前提条件是:

public class MainActivity extends Activity implements OnClickListener{}

 

第三种方法例子:

publicclass MainActivity extends Activity implements OnClickListener{

   private TextView Mytext = null;

   private TextView Mytext1 = null;

   @Override

   //第一次运行activity时会运行

   protectedvoid onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.activity_main);//设置要显示的控件

      findViewById(R.id.button1).setOnClickListener(this);

      findViewById(R.id.button2).setOnClickListener(this);

      Mytext = (TextView)findViewById(R.id.textView1);

      Mytext1 = (TextView)findViewById(R.id.textView2);

      }

  

 

      @Override

      publicvoid onClick(View arg0) {

         // TODO自动生成的方法存根

         if(arg0.getId()==R.id.button1){

            Mytext.setText("hello!");//设置文档的显示

         }

         elseif(arg0.getId()==R.id.button2){

            Mytext1.setText("hello!");//设置文档的显示

         }

      }

 

前两种方法的例子:

public class MainActivity extends Activity {

   private Button Mybutton = null;

   private Button Mybutton1 = null;

   private TextView Mytext = null;

   private TextView Mytext1 = null;

   @Override

   //第一次运行activity时会运行

   protected void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.activity_main);//设置要显示的控件

      Mybutton = (Button)findViewById(R.id.button1);

      Mytext = (TextView)findViewById(R.id.textView1);

      Mytext1 = (TextView)findViewById(R.id.textView2);

//第二种方法

      Mybutton.setOnClickListener(new OnClickListener(){

         @Override

         public void onClick(View v) {

          //TODO Auto-generated method stub

            Mytext.setText("hello!");//设置文档的显示

         }

         });

      }

//第一种方法

   public void button2OnClick(View view){

      Mytext1.setText("hello!");//设置文档的显示

   }

//第一种方法要在button的布局文件中设置android:onClick="buttonOnClick"

 

更多相关文章

  1. Android 触摸手势基础 官方文档概览
  2. Android 3.0 r1中文API文档(103) —— InputMethodManager [输入
  3. Android 3.0 r1 API中文文档(108) ―― ExpandableListAdapter
  4. Android Chronometer控件实现计时器函数详解
  5. Android下使用dlopen函数动态调用.so链接库
  6. 移动Android开发-规范文档
  7. 【转官方】Android ADB调试命令、支持的命令、ADB文档
  8. android webview实现唤起qq临时会话
  9. Android ListActivity实现遍历文件列表,查看文档类文件

随机推荐

  1. 不能调用确定性可视性()从未看到PID的连
  2. android 百度地图app key 230 错误解决
  3. jni读取assets资源文件
  4. android studio 无法调试安装到小米手机
  5. 一篇不错的Android Audio架构代码梳理总
  6. [置顶] Animation之TranslateAnim
  7. OpenCV Android 打开前置后置摄像头
  8. android 百度地图路线规划去掉节点图标
  9. android插件apk杂记
  10. Android记录4--自定义ToggleButton+用Sha