zoukankan      html  css  js  c++  java
  • 08 常见事件响应的实现方式对比

    常见的事件:

    1、单击事件onClickListener

    2、长按事件onLongClickListener

    3、滑动事件onTouchListener

    4、键盘事件onKeyListener

    5、焦点事件setOnFocusChangeListener

    常见的事件相应实现方式:

    1、动态设置

    例子:OnClickListener

    main.xml:

    <Button

            android:id="@+id/button1"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="Button"

    />

    TWOAndroidActivity.java:

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

    button.setOnClickListener(new OnClickListener() {

        //button按钮绑定了一个listener,实现button按钮点击出现效果,执行onClick方法

    public void onClick(View v) {

    Toast.makeText(TWOAndroidActivity.this, "button被点击了", Toast.LENGTH_SHORT).show();

    }

    });

    2、配置方式  仅限于onclick

    main.xml:

    <Button

            android:id="@+id/button1"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:text="@string/msg"

            android:onClick="test"

    />

    TWOAndroidActivity.java:

    Public void test(View view){

        view.setX(view.getX()+10);

    }

    3、可复用方式(在onclick方法中判断哪个组件发生了相应事件)

    Main.xml:

        <Button

            android:id="@+id/button1"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:text="Button"

            android:onClick="test"

            />

    TWOAndroidActivity.java    

    public void onCreate(Bundle savedInstanceState)

    {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.main);

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

            button1.setOnClickListener(myListener);

    }

    private View.OnClickListener myListener =new OnClickListener()

    {

    public void onClick(View v)

        {

    Toast.makeText(TWOAndroidActivity.this, "button1被点击了", Toast.LENGTH_SHORT).show();

    }

    };

    好处:适用于多个button:

    main.xml:

        <Button

            android:id="@+id/button1"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:text="Button1" 

            android:onClick="test"/>

        <Button

            android:id="@+id/button2"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="Button2" />

        <Button

            android:id="@+id/button3"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="Button3" />

    TWOAndroidActivity.java :

    public class TWOAndroidActivity extends Activity {

        @Override

        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.main);

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

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

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

            button1.setOnClickListener(myListener);

            button2.setOnClickListener(myListener);

            button3.setOnClickListener(myListener);

        }         

        private View.OnClickListener myListener =new OnClickListener(){

    public void onClick(View v) {

    Toast.makeText(TWOAndroidActivity.this, "button被点击了", Toast.LENGTH_SHORT).show();

    switch(v.getId()){

    case R.id.button1:Toast.makeText(TWOAndroidActivity.this, "button1被点击了", Toast.LENGTH_SHORT).show();

    break;

        case R.id.button2:Toast.makeText(TWOAndroidActivity.this, "button2被点击了", Toast.LENGTH_SHORT).show();

        break;

        case R.id.button3:Toast.makeText(TWOAndroidActivity.this, "button3被点击了", Toast.LENGTH_SHORT).show();

        break;

    }

        }

        };

        

    }

    运行结果:三个button只需要一个listener即可。

      

  • 相关阅读:
    使用Windows Live Writer发布日志
    下雪
    Oracle中拼出树型结构
    [转载]Javascript中最常用的55个经典技巧
    博客访问者来自15个国家和地区
    [转载]一个帐号同一时间只能一个人登录
    换了博客的皮肤
    常见的开源软件许可
    java5中的Arrays
    青花瓷
  • 原文地址:https://www.cnblogs.com/cxm-weiniss/p/7199178.html
Copyright © 2011-2022 走看看