android - 在 Android 按钮中实现滑动和单击

标签 android onclicklistener swipe-gesture

我是 android 开发的新手,我尝试为 Activity 实现滑动(使用 Ontouch)并为 Activity 中的按钮实现点击事件。如果我在按钮顶部滑动,则不会生成滑动事件。

如果我为按钮添加滑动,则不会生成按钮的 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

请帮我解决这个问题。

问候, 类纳特。

最佳答案

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

将此添加到 Activity 的 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 - 在 Android 按钮中实现滑动和单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15599836/

相关文章:

jQuery(滑动与触摸)pageX 和 pageY 不断返回 0

android - 我似乎无法让 android studio 运行,尝试更改路径,重新安装 java(oracle) 并重新安装 android

android - Android中如何高效存储位图?

Android升级sqlite数据库问题

android - 父 subview 的单击事件

android - ListView 获取点击的 View 和相应的行

android - 从 onclicklistener 开始 Intent

android - 如何在 Android 中重建优化框架

android - 创建从左向右和从右向左滑动的简单手势

ios - swift :Segmented control with a swipe gesture between views