You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Dialog主题Popup Activity中EditText无法编辑问题求助

Fix EditText Not Editable in Dialog-Themed Activity

I’ve run into this exact issue before with dialog-themed Activities—those tricky little problems where the EditText looks fully functional but won’t let you type anything. Let’s break down why your current approach isn’t working and fix it step by step.

Common Culprits & Targeted Fixes

1. Fix Window Focus & Soft Input Behavior

Dialog-themed Activities often get marked as non-focusable by default, which blocks EditTexts from receiving input. Add these lines right after setContentView in your onCreate method:

// Temporarily toggle non-focusable flag to reset window focus state
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
// Ensure soft keyboard appears and adjusts the window properly
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

Also, your EditTexts need both focusable and focusableInTouchMode enabled (just setFocusable(true) isn’t enough for touch-based input):

EditText title = (EditText) findViewById(R.id.bug_subject);
title.setFocusable(true);
title.setFocusableInTouchMode(true);
title.requestFocus(); // Now this will actually trigger focus

EditText content = (EditText) findViewById(R.id.bug_content);
content.setFocusable(true);
content.setFocusableInTouchMode(true);

2. Switch to AppCompat Dialog Theme (Critical for AppCompatActivity)

Since you’re extending AppCompatActivity, using the old system Theme.Dialog can cause compatibility issues with focus handling. Update your AndroidManifest entry for BugActivity:

<activity
    android:name=".BugActivity"
    android:theme="@style/Theme.AppCompat.Dialog">
</activity>

AppCompat themes are optimized for modern Android components and will avoid many hidden focus-related bugs.

3. Move Window Layout Adjustments to onCreate

Your onAttachedToWindow method modifies the window layout after it’s already attached to the screen, which can interfere with focus initialization. Move those lines into onCreate instead:

// Inside onCreate, after setting up EditTexts
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int wi = dm.widthPixels;
int hi = dm.heightPixels;
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.width = (int) (wi * .84);
lp.height = (int) (hi * .8);
lp.gravity = Gravity.CENTER | Gravity.TOP;
lp.y = 10;
getWindow().setAttributes(lp);

This ensures the window is fully configured before any focus requests are processed.

Modified Full Activity Code

Here’s how your BugActivity should look with all fixes applied:

public class BugActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_bug);

        // Fix window focus and soft input behavior
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        // Setup EditTexts with proper focus settings
        EditText title = (EditText) findViewById(R.id.bug_subject);
        title.setFocusable(true);
        title.setFocusableInTouchMode(true);
        title.setText("");
        title.requestFocus();

        EditText content = (EditText) findViewById(R.id.bug_content);
        content.setFocusable(true);
        content.setFocusableInTouchMode(true);

        // Adjust window size and position
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int wi = dm.widthPixels;
        int hi = dm.heightPixels;
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = (int) (wi * .84);
        lp.height = (int) (hi * .8);
        lp.gravity = Gravity.CENTER | Gravity.TOP;
        lp.y = 10;
        getWindow().setAttributes(lp);
    }

    // Remove the onAttachedToWindow method since we moved its logic to onCreate
}

These changes should resolve the focus issue and let you type into the EditTexts normally.

内容的提问来源于stack exchange,提问作者Killua San

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 03:55:57