showKeywordDialog: function() {
if (isKeywordDialogShowing) return;
isKeywordDialogShowing = true;

threads.start(function() {
    let dialogLayout = (
        <frame>
            <card cardBackgroundColor="#f5f5f5" cardCornerRadius="8dp" cardElevation="4dp" margin="8" layout_gravity="center">
                <vertical padding="16" bg="#f5f5f5" w="300">
                    <text text="修改关键词配置" textSize="18sp" textColor="#333333" marginBottom="12" gravity="center"/>
                    
                    <text text="当前关键词:" textSize="14sp" textColor="#555555" marginTop="8"/>
                    <text id="currentKeywords" text={currentConfig.keyword.join(", ")} textSize="14sp" textColor="#2196F3" marginBottom="12"/>
                    
                    <text text="输入新关键词(用逗号分隔):" textSize="14sp" textColor="#555555" marginTop="8"/>
                    <input id="keywordsInput" text={currentConfig.keyword.join(", ")} textSize="14sp" 
                        focusable="true" focusableInTouchMode="true"/>
                    
                    <horizontal gravity="center" marginTop="20">
                        <button id="confirmBtn" text="确认" w="110" h="40" marginRight="10" bg="#4CAF50" textColor="#ffffff" elevation="2dp"/>
                        <button id="cancelBtn" text="取消" w="110" h="40" bg="#ffffff" textColor="#757575" elevation="2dp" style="Widget.AppCompat.Button.Borderless"/>
                    </horizontal>
                </vertical>
            </card>
        </frame>
    );
    
    keywordDialog = floaty.window(dialogLayout);
    positions = calculateWindowPosition();
    keywordDialog.setPosition(positions.dialog.x, positions.dialog.y);
    
    let confirmBtn = keywordDialog.findView("confirmBtn");
    let cancelBtn = keywordDialog.findView("cancelBtn");
    let keywordsInput = keywordDialog.findView("keywordsInput");
    
    // 添加输入法自动弹出逻辑
    ui.run(() => {
        keywordsInput.requestFocus();
        let imm = context.getSystemService(android.content.Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(keywordsInput, android.view.inputmethod.InputMethodManager.SHOW_IMPLICIT);
    });
    
    confirmBtn.click(() => {
        let input = keywordsInput.text();
        if (input) {
            currentConfig.keyword = input.split(",").map(k => k.trim()).filter(k => k);
            console.log("✅ 关键词已更新:", currentConfig.keyword.join(", "));
        }
        ui.run(() => {
            // 关闭输入法
            let imm = context.getSystemService(android.content.Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(keywordsInput.getWindowToken(), 0);
            
            try { keywordDialog.close(); } catch (e) {}
            keywordDialog = null;
            isKeywordDialogShowing = false;
        });
    });
    
    cancelBtn.click(() => {
        ui.run(() => {
            // 关闭输入法
            let imm = context.getSystemService(android.content.Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(keywordsInput.getWindowToken(), 0);
            
            try { keywordDialog.close(); } catch (e) {}
            keywordDialog = null;
            isKeywordDialogShowing = false;
        });
    });
});

},
无法弹出输入法

已存档
xv7j3ndpak · 1 个月前

回答

Hamibot AI
1
· 1 个月前

根据你提供的代码,无法弹出输入法的原因可能有以下几点:

  1. floaty.window 的限制:使用 floaty.window 创建的窗口可能不支持直接弹出输入法。你可以尝试使用 dialogs.build() 来创建对话框,而不是使用 floaty.window

  2. 上下文问题:确保 context 是有效的 Android 上下文对象。如果 context 未正确初始化,可能会导致输入法无法弹出。

  3. 权限问题:确保你的脚本已经获取了必要的权限,例如 INPUT_METHOD_SERVICE

以下是修改后的代码示例,使用 dialogs.build() 来创建对话框并弹出输入法:

showKeywordDialog: function() {
    if (isKeywordDialogShowing) return;
    isKeywordDialogShowing = true;

    threads.start(function() {
        let dialog = dialogs.build({
            title: "修改关键词配置",
            content: (
                <vertical>
                    <text text="当前关键词:" textSize="14sp" textColor="#555555" marginTop="8"/>
                    <text id="currentKeywords" text={currentConfig.keyword.join(", ")} textSize="14sp" textColor="#2196F3" marginBottom="12"/>
                    
                    <text text="输入新关键词(用逗号分隔):" textSize="14sp" textColor="#555555" marginTop="8"/>
                    <input id="keywordsInput" text={currentConfig.keyword.join(", ")} textSize="14sp" 
                        focusable="true" focusableInTouchMode="true"/>
                </vertical>
            ),
            positive: "确认",
            negative: "取消"
        }).on("positive", function(dialog) {
            let input = dialog.findView("keywordsInput").text();
            if (input) {
                currentConfig.keyword = input.split(",").map(k => k.trim()).filter(k => k);
                console.log("✅ 关键词已更新:", currentConfig.keyword.join(", "));
            }
            dialog.dismiss();
            isKeywordDialogShowing = false;
        }).on("negative", function(dialog) {
            dialog.dismiss();
            isKeywordDialogShowing = false;
        });

        dialog.show();

        // 弹出输入法
        let keywordsInput = dialog.findView("keywordsInput");
        keywordsInput.requestFocus();
        let imm = context.getSystemService(android.content.Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(keywordsInput, android.view.inputmethod.InputMethodManager.SHOW_IMPLICIT);
    });
}

说明:

  • 使用 dialogs.build() 创建对话框,而不是 floaty.window
  • positivenegative 事件中处理按钮点击逻辑。
  • 在对话框显示后,通过 requestFocus()showSoftInput() 弹出输入法。

如果问题仍然存在,请检查你的脚本是否正确获取了 context 对象,并确保你的环境支持输入法弹出功能。

ℹ️ 本回答由 AI 生成,内容仅供参考