在Android按钮中实现滑动和点击 [英] Implementing Swipe and Click in Android Button

查看:34
本文介绍了在Android按钮中实现滑动和点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android开发的新手,我试图实现活动的Swipe(使用OnTouch)和活动中按钮的点击事件。如果我在按钮上方滑动,则不会生成滑动事件。

如果我为按钮添加Swipe,则不会生成按钮的onClick事件。

要解决这个问题,我应该做些什么?

如有任何澄清,请向我咨询。

布局编码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/default_2x"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".LearnUpperCaseActivity" >

    <LinearLayout
        android:id="@+id/content_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="@string/learn_uppercase"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="11"
            android:background="@drawable/main_area_blue_2x" >

            <TextView
                android:id="@+id/question"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:gravity="center"
                android:text="@string/hard_coded_char" />
        </RelativeLayout>

        <TextView
            android:id="@+id/instruction"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/tab_below" />

        <RelativeLayout
            android:id="@+id/button_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="9"
            android:padding="3dp" >

            <LinearLayout
                android:id="@+id/row1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/button1"
                    style="@style/UpperCaseButton"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_margin="3dp"
                    android:layout_weight="1"
                    android:background="@drawable/blue_button"
                    android:onClick="button1Pressed"
                    android:text="@string/hard_coded_char" >
                </Button>

                <Button
                    android:id="@+id/button2"
                    style="@style/UpperCaseButton"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_margin="3dp"
                    android:layout_weight="1"
                    android:background="@drawable/brown_button_2x"
                    android:onClick="button2Pressed"
                    android:text="@string/hard_coded_char" >
                </Button>

                <Button
                    android:id="@+id/button3"
                    style="@style/UpperCaseButton"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_margin="3dp"
                    android:layout_weight="1"
                    android:background="@drawable/dark_green_2x"
                    android:onClick="button3Pressed"
                    android:text="@string/hard_coded_char" >
                </Button>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/row2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/row1"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/button4"
                    style="@style/UpperCaseButton"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_margin="3dp"
                    android:layout_weight="1"
                    android:background="@drawable/fluora_button_2x"
                    android:onClick="button4Pressed"
                    android:text="@string/hard_coded_char" >
                </Button>

                <Button
                    android:id="@+id/button5"
                    style="@style/UpperCaseButton"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_margin="3dp"
                    android:layout_weight="1"
                    android:background="@drawable/green_button_2x"
                    android:onClick="button5Pressed"
                    android:text="@string/hard_coded_char" >
                </Button>

                <Button
                    android:id="@+id/button6"
                    style="@style/UpperCaseButton"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_margin="3dp"
                    android:layout_weight="1"
                    android:background="@drawable/maroon_2x"
                    android:onClick="button6Pressed"
                    android:text="@string/hard_coded_char" >
                </Button>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/row3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/row2"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/button7"
                    style="@style/UpperCaseButton"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_margin="3dp"
                    android:layout_weight="1"
                    android:background="@drawable/yellow_button_2x"
                    android:onClick="button7Pressed"
                    android:text="@string/hard_coded_char" >
                </Button>

                <Button
                    android:id="@+id/button8"
                    style="@style/UpperCaseButton"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_margin="3dp"
                    android:layout_weight="1"
                    android:background="@drawable/purple_button_2x"
                    android:onClick="button8Pressed"
                    android:text="@string/hard_coded_char" >
                </Button>

                <Button
                    android:id="@+id/button9"
                    style="@style/UpperCaseButton"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_margin="3dp"
                    android:layout_weight="1"
                    android:background="@drawable/pink_button_2x"
                    android:onClick="button9Pressed"
                    android:text="@string/hard_coded_char" >
                </Button>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/row4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/row3"
                android:gravity="center"
                android:orientation="horizontal" >

                <Button
                    android:id="@+id/home"
                    style="@style/UpperCaseButton"
                    android:layout_width="150dp"
                    android:layout_height="30dp"
                    android:layout_margin="3dp"
                    android:background="@drawable/blue_long_button_2x"
                    android:onClick="home"
                    android:text="@string/home" >
                </Button>
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>
</RelativeLayout>

查看此链接中的图像。https://sites.google.com/site/temptemptemp123tempqwe/doubt.jpg?attredirects=0

请帮助我解决此问题。

问候, 贝内特。

推荐答案

您只需在按下并释放按钮的同时处理坐标之间的差异。

将此添加到您的活动的onCreate()中:

     protected void onCreate(Bundle savedInstanceState) {
            final float len = getResources().getDisplayMetrics().densityDpi/6;
            OnTouch Listener controlButtonTouchListener=new View.OnTouchListener() {
                float initX;
                float initY;
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch(event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            initX = event.getX();
                            initY = event.getY();
                            break;
                        case MotionEvent.ACTION_UP:
                            initX -= event.getX(); //now has difference value
                            initY -= event.getY(); //now has difference value
                            if (initY > len) {
                                ((Button)v).setText("Swipe up");
                                //Toast.makeText(context,"Swipe up",Toast.LENGTH_SHORT).show();
                            } else if(initY < -len) {
                                ((Button)v).setText("Swipe Down");
                                //Toast.makeText(context,"Swipe Down",Toast.LENGTH_SHORT).show();
                            } else {
                                if(initY < 0) initY = -initY;
                                if(initX < 0) initX = -initX;
                                if(initX <= len/4 && initY <= len/4) {
                                    ((Button)v).setText("Clicked");
                                }
                            }
                            break;
                    }
                    return true;
                }
            };
            findViewById(R.id.ClickSwipeButton).setOnTouchListener(controlButtonTouchListener);
        }

这篇关于在Android按钮中实现滑动和点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆