Add a sample for handling hover events.
Change-Id: I9e19e09d3f5b8d0fd03f52cc8d88e6a12399d12f
This commit is contained in:
106
samples/ApiDemos/src/com/example/android/apis/view/Hover.java
Normal file
106
samples/ApiDemos/src/com/example/android/apis/view/Hover.java
Normal file
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* Copyright (C) 2011 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.example.android.apis.view;
|
||||
|
||||
import com.example.android.apis.R;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.os.Bundle;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.CompoundButton;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
||||
/**
|
||||
* Demonstrates how to use {@link View#onHoverEvent}, {@link ViewGroup#onInterceptHoverEvent},
|
||||
* and {@link View#setOnHoverListener}.
|
||||
*
|
||||
* This activity displays a few buttons and text fields and entices the user
|
||||
* to hover over them using a mouse or touch pad. It displays feedback reporting
|
||||
* the position of the pointing device and the label of the view being hovered.
|
||||
*
|
||||
* A button changes from dark green to bright yellow when hovered.
|
||||
* This effect is achieved by using a state-list drawable to select among different
|
||||
* background shapes and colors based on the hover state of the button.
|
||||
*
|
||||
* A {@link View#OnHoverEventListener} is used to listen for hover events within the
|
||||
* container. The container will re
|
||||
*
|
||||
* A checkbox is used to control whether a special view, the Interceptor, will intercept
|
||||
* events before they are sent to its child (a button). When the Interceptor
|
||||
* is intercepting events, the button will not change state as the pointer hovers
|
||||
* over it because the interceptor itself will grab the events.
|
||||
*/
|
||||
public class Hover extends Activity {
|
||||
private TextView mMessageTextView;
|
||||
private CheckBox mInterceptCheckBox;
|
||||
private HoverInterceptorView mInterceptor;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
setContentView(R.layout.hover);
|
||||
|
||||
mMessageTextView = (TextView) findViewById(R.id.message);
|
||||
mInterceptCheckBox = (CheckBox) findViewById(R.id.intercept_checkbox);
|
||||
mInterceptor = (HoverInterceptorView) findViewById(R.id.interceptor);
|
||||
|
||||
View container = findViewById(R.id.container);
|
||||
container.setOnHoverListener(new View.OnHoverListener() {
|
||||
@Override
|
||||
public boolean onHover(View v, MotionEvent event) {
|
||||
switch (event.getAction()) {
|
||||
case MotionEvent.ACTION_HOVER_ENTER:
|
||||
mMessageTextView.setText(Hover.this.getResources().getString(
|
||||
R.string.hover_message_entered_at,
|
||||
event.getX(), event.getY()));
|
||||
break;
|
||||
case MotionEvent.ACTION_HOVER_MOVE:
|
||||
mMessageTextView.setText(Hover.this.getResources().getString(
|
||||
R.string.hover_message_moved_at,
|
||||
event.getX(), event.getY()));
|
||||
break;
|
||||
case MotionEvent.ACTION_HOVER_EXIT:
|
||||
mMessageTextView.setText(Hover.this.getResources().getString(
|
||||
R.string.hover_message_exited_at,
|
||||
event.getX(), event.getY()));
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
mInterceptCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
||||
@Override
|
||||
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
||||
mInterceptor.setInterceptHover(isChecked);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright (C) 2011 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.example.android.apis.view;
|
||||
|
||||
import com.example.android.apis.R;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.MotionEvent;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
/**
|
||||
* Part of the {@link Hover} demo activity.
|
||||
*
|
||||
* The Interceptor view is a simple subclass of LinearLayout whose sole purpose
|
||||
* is to override {@link #onInterceptHoverEvent}. When the checkbox in the
|
||||
* hover activity is checked, the interceptor view will intercept hover events.
|
||||
*
|
||||
* When this view intercepts hover events, its children will not receive
|
||||
* hover events. This can be useful in some cases when implementing a custom
|
||||
* view group that would like to prevent its children from being hovered
|
||||
* under certain situations. Usually such custom views will be much more
|
||||
* interesting and complex than our little Interceptor example here.
|
||||
*/
|
||||
public class HoverInterceptorView extends LinearLayout {
|
||||
private boolean mInterceptHover;
|
||||
|
||||
public HoverInterceptorView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onInterceptHoverEvent(MotionEvent event) {
|
||||
if (mInterceptHover) {
|
||||
return true;
|
||||
}
|
||||
return super.onInterceptHoverEvent(event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onHoverEvent(MotionEvent event) {
|
||||
TextView textView = (TextView) findViewById(R.id.intercept_message);
|
||||
if (mInterceptHover && event.getAction() != MotionEvent.ACTION_HOVER_EXIT) {
|
||||
textView.setText(getResources().getString(
|
||||
R.string.hover_intercept_message_intercepted));
|
||||
return true;
|
||||
}
|
||||
textView.setText(getResources().getString(
|
||||
R.string.hover_intercept_message_initial));
|
||||
return super.onHoverEvent(event);
|
||||
}
|
||||
|
||||
public void setInterceptHover(boolean intercept) {
|
||||
mInterceptHover = intercept;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user