点击事件

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;

          }

          }

 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/185792.html

(0)
上一篇 2021年11月4日
下一篇 2021年11月4日

相关推荐

发表回复

登录后才能评论