• 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即可。

      

  • 相关阅读:
    Git与GitHub(利用git上传本地文件到GitHub上面)
    PHP之上传文件upload.php
    PHP之数据库连接配置文件
    MUI 之picker,dialog,a标签——刷新页面问题(保留picker选中的数据)
    MUI自定义select down 下拉框
    Google按ESC退出全屏(带iframe网站)解决问题方案
    提高开发效率 -> 图片
    sublime text
    头脑风暴
    http://www.uupoop.com/ps/
  • 原文地址:https://www.cnblogs.com/cxm-weiniss/p/7199178.html
Copyright © 2020-2023  润新知