fix: EditText of floaty window cannot request focus

This commit is contained in:
hyb1996
2018-03-20 22:09:51 +08:00
parent b46e08e6bf
commit 9e4cdb4723
4 changed files with 117 additions and 14 deletions

View File

@@ -1,6 +1,6 @@
var window = floaty.window(
<vertical>
<input id="input" text="请输入你的名字" textSize="16sp" />
<input id="input" text="请输入你的名字" textSize="16sp" focusable="true"/>
<button id="ok" text="确定"/>
</vertical>
);
@@ -9,12 +9,25 @@ window.exitOnClose();
toast("长按确定键可调整位置");
window.input.on("key", function(keyCode, event){
if(event.getAction() == event.ACTION_DOWN && keyCode == keys.back){
window.disableFocus();
event.consumed = true;
}
});
window.input.on("touch_down", ()=>{
window.requestFocus();
window.input.requestFocus();
});
window.ok.on("click", ()=>{
toast("傻瓜! " + window.input.text());
window.disableFocus();
});
window.ok.on("long_click", ()=>{
window.setAdjustEnabled(!window.isAdjustEnabled());
});
setInterval(()=>{}, 1000);
setInterval(()=>{}, 1000);

View File

@@ -122,6 +122,9 @@ module.exports = function(__runtime__, scope){
}
});
view.setOnTouchListener(function(v, event){
if(gestureDetector.onTouchEvent(event)){
return true;
}
event = wrapMotionEvent(event);
event.consumed = false;
scope.__exitIfError__(function(){
@@ -140,6 +143,13 @@ module.exports = function(__runtime__, scope){
view.setOnClickListener(function(v){
view.emit("click", view);
});
view.setOnKeyListener(function(v, keyCode, event){
event = wrapMotionEvent(event);
scope.__exitIfError__(function(){
view.emit("key", keyCode, event, v);
});
return event.consumed;
});
if(typeof(view.setOnCheckedChangeListener) == 'function'){
view.setOnCheckedChangeListener(function(v, isChecked){
view.emit("check", isChecked, view);

View File

@@ -1,16 +1,21 @@
package com.stardust.autojs.core.floaty;
import android.graphics.PixelFormat;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.FrameLayout;
import com.stardust.autojs.R;
import com.stardust.enhancedfloaty.FloatyService;
import com.stardust.enhancedfloaty.ResizableFloaty;
import com.stardust.enhancedfloaty.ResizableFloatyWindow;
import com.stardust.enhancedfloaty.WindowBridge;
import com.stardust.enhancedfloaty.gesture.DragGesture;
import com.stardust.enhancedfloaty.gesture.ResizeGesture;
/**
* Created by Stardust on 2017/12/5.
@@ -19,17 +24,21 @@ import com.stardust.enhancedfloaty.ResizableFloatyWindow;
public class FloatyWindow extends ResizableFloatyWindow {
private final Object mLock = new Object();
private View mView;
private boolean mCreated = false;
private View mMoveCursor;
private View mResizer;
private View mCloseButton;
private static final String TAG = "ResizableFloatyWindow";
private WindowManager mWindowManager;
private WindowManager.LayoutParams mWindowLayoutParams;
private ViewGroup mWindowView;
private View mRootView;
private View mResizer;
private View mMoveCursor;
private WindowBridge mWindowBridge;
private MyFloaty mFloaty;
public FloatyWindow(View view) {
this(new MyFloaty(view));
mView = view;
}
private FloatyWindow(MyFloaty floaty) {
@@ -52,11 +61,15 @@ public class FloatyWindow extends ResizableFloatyWindow {
@Override
public void onCreate(FloatyService service, WindowManager manager) {
super.onCreate(service, manager);
View root = (View) mView.getParent().getParent();
mMoveCursor = root.findViewById(R.id.move_cursor);
mResizer = root.findViewById(R.id.resizer);
mCloseButton = root.findViewById(R.id.close);
this.mWindowManager = manager;
this.mWindowLayoutParams = this.createWindowLayoutParams();
if (this.mFloaty == null) {
throw new IllegalStateException("Must start this service by static method ResizableExpandableFloatyWindow.startService");
} else {
this.initWindowView(service);
this.mWindowBridge = new WindowBridge.DefaultImpl(this.mWindowLayoutParams, this.mWindowManager, this.mWindowView);
this.initGesture();
}
synchronized (mLock) {
mCreated = true;
mLock.notify();
@@ -84,7 +97,66 @@ public class FloatyWindow extends ResizableFloatyWindow {
}
public View getRootView() {
return mFloaty.mRootView;
return mRootView;
}
private void initWindowView(FloatyService service) {
this.mWindowView = (ViewGroup) View.inflate(service, com.stardust.lib.R.layout.ef_floaty_container, (ViewGroup) null);
this.mRootView = this.mFloaty.inflateView(service, this);
this.mResizer = this.mFloaty.getResizerView(this.mRootView);
this.mMoveCursor = this.mFloaty.getMoveCursorView(this.mRootView);
this.mCloseButton = mRootView.findViewById(R.id.close);
android.view.ViewGroup.LayoutParams params = new android.view.ViewGroup.LayoutParams(-2, -2);
this.mWindowView.addView(this.mRootView, params);
this.mWindowView.setFocusableInTouchMode(true);
this.mWindowManager.addView(this.mWindowView, this.mWindowLayoutParams);
}
private WindowManager.LayoutParams createWindowLayoutParams() {
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
PixelFormat.TRANSLUCENT);
layoutParams.gravity = Gravity.TOP | Gravity.START;
return layoutParams;
}
public void disableWindowFocus() {
mWindowLayoutParams.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
mWindowManager.updateViewLayout(mWindowView, mWindowLayoutParams);
}
public void requestWindowFocus() {
mWindowLayoutParams.flags &= ~WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
mWindowManager.updateViewLayout(mWindowView, mWindowLayoutParams);
mWindowView.requestFocus();
}
private void initGesture() {
if (this.mResizer != null) {
ResizeGesture.enableResize(this.mResizer, this.mRootView, this.mWindowBridge);
}
if (this.mMoveCursor != null) {
DragGesture gesture = new DragGesture(this.mWindowBridge, this.mMoveCursor);
gesture.setPressedAlpha(1.0F);
}
}
public WindowBridge getWindowBridge() {
return this.mWindowBridge;
}
public void onServiceDestroy(FloatyService service) {
this.close();
}
public void close() {
this.mWindowManager.removeView(this.mWindowView);
FloatyService.removeWindow(this);
}
private static class MyFloaty implements ResizableFloaty {

View File

@@ -151,6 +151,14 @@ public class Floaty {
mExitOnClose = true;
}
public void requestFocus() {
mWindow.requestWindowFocus();
}
public void disableFocus() {
mWindow.disableWindowFocus();
}
public void close() {
close(true);
}