借助手势显示按钮

时间:2017-07-24 18:21:15

标签: java android button gestures

在我的活动中是一个不可见的动作按钮。现在我想在点击或滑动时显示他。

我的XML文件:

    <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:visibility="visible">

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:ignore="ScrollViewSize">

    <RelativeLayout
        android:id="@+id/activity_a1"
        android:layout_width="match_parent"
        android:layout_height="625dp"
        android:background="@color/background"
        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="com.thebest.charakterisierung.a1"
        tools:ignore="UselessParent">


        <android.support.design.widget.FloatingActionButton
            android:id="@+id/floatingActionButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:clickable="true"
            android:visibility="inviible"
            app:backgroundTint="@android:color/holo_green_dark"
            app:fabSize="mini"
            app:srcCompat="@android:drawable/ic_menu_save" />
    </RelativeLayout>
</LinearLayout>

我的java文件:

package com.test.test;
import android.content.SharedPreferences;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.view.MotionEvent;

public class Geschichte1 extends AppCompatActivity implements                 GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener {
private GestureDetectorCompat GestureDetect;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(android.view.Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,   WindowManager.LayoutParams.FLAG_FULLSCREEN);
    this.setContentView(R.layout.activity_a1);
    Button btn =(Button)findViewById(R.id.floatingActionButton);
    GestureDetect = new GestureDetectorCompat(this, this);
    GestureDetect.setOnDoubleTapListener(this);
    }


@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
            return false;
}

@Override
public boolean onDoubleTap(MotionEvent e) {
    return false;
}

@Override
public boolean onDoubleTapEvent(MotionEvent e) {
    return false;
}

@Override
public boolean onDown(MotionEvent e) {
    return false;
}

@Override
public void onShowPress(MotionEvent e) {

}

@Override
public boolean onSingleTapUp(MotionEvent e) {
    return false;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    return false;
}

@Override
public void onLongPress(MotionEvent e) {

}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,     float velocityY) {
    return false;
}

}

也许我可以添加java文件:Button.setVisibility或Button.Visible?没什么我尝试过的工作。

1 个答案:

答案 0 :(得分:0)

在xml文件更新行中 机器人:能见度= “inviible”

要 机器人:能见度= “看不见的”

在java文件中,点击或滑动时使按钮可见

使btn变量类变量可以在onDoubleTap或onSingleTapConfirmed中访问 按钮btn;

将按钮的可见性设置为可见使用: btn.setVisibility(View.VISIBLE);