|
30 | 30 | import android.animation.ObjectAnimator; |
31 | 31 | import android.annotation.TargetApi; |
32 | 32 | import android.content.Context; |
| 33 | +import android.content.pm.PackageManager; |
33 | 34 | import android.graphics.Color; |
34 | 35 | import android.graphics.PointF; |
35 | 36 | import android.graphics.RectF; |
|
38 | 39 | import android.os.Build; |
39 | 40 | import android.util.Log; |
40 | 41 | import android.view.Gravity; |
| 42 | +import android.view.KeyEvent; |
41 | 43 | import android.view.LayoutInflater; |
42 | 44 | import android.view.MotionEvent; |
43 | 45 | import android.view.View; |
@@ -206,10 +208,14 @@ public void show() { |
206 | 208 | mRootView.post(new Runnable() { |
207 | 209 | @Override |
208 | 210 | public void run() { |
209 | | - if (mRootView.isShown()) |
| 211 | + if (mRootView.isShown()) { |
210 | 212 | mPopupWindow.showAtLocation(mRootView, Gravity.NO_GRAVITY, mRootView.getWidth(), mRootView.getHeight()); |
211 | | - else |
| 213 | + if (mFocusable) |
| 214 | + mContentLayout.requestFocus(); |
| 215 | + } |
| 216 | + else { |
212 | 217 | Log.e(TAG, "Tooltip cannot be shown, root view is invalid or has been closed."); |
| 218 | + } |
213 | 219 | } |
214 | 220 | }); |
215 | 221 | } |
@@ -316,6 +322,31 @@ private void configContentView() { |
316 | 322 |
|
317 | 323 | mContentLayout = linearLayout; |
318 | 324 | mContentLayout.setVisibility(View.INVISIBLE); |
| 325 | + |
| 326 | + if (mFocusable) |
| 327 | + { |
| 328 | + mContentLayout.setFocusableInTouchMode(true); |
| 329 | + mContentLayout.setOnKeyListener(new View.OnKeyListener() |
| 330 | + { |
| 331 | + @Override |
| 332 | + public boolean onKey(View v, int keyCode, KeyEvent event) |
| 333 | + { |
| 334 | + if (event.getAction() == KeyEvent.ACTION_UP) { |
| 335 | + switch (keyCode) { |
| 336 | + // The following are taken from the KeyEvent.isConfirmKey() function |
| 337 | + case KeyEvent.KEYCODE_DPAD_CENTER: |
| 338 | + case KeyEvent.KEYCODE_ENTER: |
| 339 | + case KeyEvent.KEYCODE_SPACE: |
| 340 | + case KeyEvent.KEYCODE_NUMPAD_ENTER: |
| 341 | + dismiss(); |
| 342 | + return true; |
| 343 | + } |
| 344 | + } |
| 345 | + return false; |
| 346 | + } |
| 347 | + }); |
| 348 | + } |
| 349 | + |
319 | 350 | mPopupWindow.setContentView(mContentLayout); |
320 | 351 | } |
321 | 352 |
|
@@ -579,6 +610,7 @@ public static class Builder { |
579 | 610 |
|
580 | 611 | public Builder(Context context) { |
581 | 612 | this.context = context; |
| 613 | + this.focusable = !(context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN)); |
582 | 614 | } |
583 | 615 |
|
584 | 616 | public SimpleTooltip build() throws IllegalArgumentException { |
@@ -1034,8 +1066,8 @@ public Builder onShowListener(OnShowListener onShowListener) { |
1034 | 1066 | } |
1035 | 1067 |
|
1036 | 1068 | /** |
1037 | | - * <div class="pt">Habilita o foco no conteúdo da tooltip. Padrão é <tt>false</tt>.</div> |
1038 | | - * <div class="en">Enables focus in the tooltip content. Default is <tt>false</tt>.</div> |
| 1069 | + * <div class="pt">Habilita o foco no conteúdo da tooltip. Padrão é <tt>false</tt> em dispositivos sensíveis ao toque e <tt>true</tt> em dispositivos não sensíveis ao toque.</div> |
| 1070 | + * <div class="en">Enables focus in the tooltip content. Default is <tt>false</tt> on touch devices, and <tt>true</tt> on non-touch devices.</div> |
1039 | 1071 | * |
1040 | 1072 | * @param focusable <div class="pt">Pode receber o foco.</div> |
1041 | 1073 | * <div class="en">Can receive focus.</div> |
|
0 commit comments