• 从零开始学android开发-View的setOnClickListener的添加方法


    1)第一种,也是最长见的添加方法(一下都以Button为例)

    Button btn = (Button) findViewById(R.id.myButton);
     btn .setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
     //do something
             }
         });

    2)第二种,下面这个方法较前一种稍微简单了一些,允许多个Buttons共享一个Listener。通过Switch控制对不同Button Click事件的响应方法:

    Button btn = (Button) findViewById(R.id.mybutton);
    Button btn2 = (Button) findViewById(R.id.mybutton2);
    btn.setOnClickListener(handler);
    btn2.setOnClickListener(handler);
    View.OnClickListener handler = View.OnClickListener() {
            public void onClick(View v) {
                switch (v.getId()) {
                   case R.id.mybutton: 
    //do something
                   break;
                   case R.id.mybutton2: 
    //do something
                   break;
                }
        }

    或者

        Button list=null;
        Button about=null;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.main);
            list=(Button)findViewById(R.id.foodlistbtn);
            about=(Button)findViewById(R.id.aboutbutton);
    //        list.setText(text);
    //        list.setCompoundDrawables(left, top, right, bottom);
            list.setOnClickListener(this);
            about.setOnClickListener(this);
            
        }
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(v.getId()==R.id.foodlistbtn){
            Intent intent=new Intent();
            intent.setClass(MainApp.this, FoodListView.class);
            startActivity(intent);
            list.setBackgroundResource(R.drawable.btn_food_list_active);
            }else if(v.getId()==R.id.aboutbutton){
                Intent intent=new Intent(this, About.class);
                startActivity(intent);
                about.setBackgroundResource(R.drawable.btn_food_about_active);
            }
        }

    3)第三种,直接将Clicklistener捆绑XML layout中的Views元素,在程序中定义的Listener方法需要带有一个View类型的参数:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TextView android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:id="@+id/text"
            android:text="@string/hello" />
        <Button android:id="@+id/mybutton" android:layout_height="wrap_content"
            android:layout_width="wrap_content" android:onClick="mybuttonlistener"></Button>
    </LinearLayout>

    java代码:

     Button btn = (Button) findViewById(R.id.mybutton);
     
     public void mybuttonlistener(View target){
     //do something
         }
  • 相关阅读:
    SVN服务器使用和搭建
    jenkins以.war包安装配置教程
    Django数据库与程序交互
    工作中常用到的linux命令
    js获取元素属性
    Jenkins利用插件持续集成的思路及安装
    web前端面试第一次[javascript函数和方法的区别]
    web前端面试第一次[定时器]
    linux卸载mysql
    mysql启动错误:Starting MySQL... ERROR! The server quit without updating PID file (/data/mysql/mysql.pid).
  • 原文地址:https://www.cnblogs.com/dekevin/p/4290120.html
Copyright © 2020-2023  润新知