首页 > 代码库 > 点击事件

点击事件

1,点击事件几种方式:

  >>1,匿名内部类

public class MainActivity extends Activity {

 

          private Button btn;

          private TextView tv_show;

 

          @Override

          protected void onCreate(Bundle savedInstanceState) {

                    super.onCreate(savedInstanceState);

                    setContentView(R.layout.activity_main);

                    btn = (Button) findViewById(R.id.btn);

                    tv_show = (TextView) findViewById(R.id.tv_show);

                    //第一种匿名内部类

                    btn.setOnClickListener(new OnClickListener() {

                               

                              @Override

                              public void onClick(View v) {

                                        tv_show.setText("好啦,马上到!!");

                                         

                              }

                    });          

          }

}

>>2,类实现的方法

    private void clickTwo() {

                    btn.setOnClickListener(this);

                     

          }

技术分享

       @Override

       public void onClick(View v) {

                     

                    tv_show.setText("好啦,马上到!!");

          }

>>3,xml 布局onClick

技术分享

 

 

/**

           * 第三种方法:第一个修饰用public 第二个方法里面 View

           * @param v

           */

          public void clickThree(View v){

                    tv_show.setText("各位同学请注意:今天下午苍老师来华育六班视察!!");

 

          }

>>4,第四种方法

    public void onClick(View v) {

                     

            switch (v.getId()) {

          case R.id.btn:

                    tv_show.setText("好啦");

                    break;

          case R.id.btn2:][

                    tv_show.setText("好啦,马上到!!");

                    break;

          case R.id.btn3:

                    tv_show.setText("好啦,马上到!!今晚华育小树林见!!");

                    break;

 

          default:

                    break;

          }

          }

 

本文出自 “xmy” 博客,请务必保留此出处http://88649164.blog.51cto.com/9890093/1610873

点击事件