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

Android多CustomAutoCompleteTextView焦点监听失效问题求助

Hey there! Let's figure out why your last two CustomAutoCompleteTextView controls aren't triggering their focus change listeners. I've tackled similar issues before, so let's break down the most likely causes and fixes step by step:

1. Double-check listener binding and view initialization

First, make sure you're properly initializing the two problematic views and attaching the OnFocusChangeListener to them. It's easy to accidentally skip this step when copying code for multiple controls.

Check your activity/fragment's initialization code (like onCreate) to confirm you have lines like this:

// Replace with your actual view IDs
actventryDivName = findViewById(R.id.actventry_div_name);
actvProductType = findViewById(R.id.actv_product_type);

// Attach the focus listener (either using 'this' if your class implements the interface, or a lambda)
actventryDivName.setOnFocusChangeListener(this);
actvProductType.setOnFocusChangeListener(this);

If you're using View Binding, ensure you're referencing the correct views from the binding object—no typos in the view IDs!

2. Verify focusable properties in XML

Sometimes views can't receive focus at all because their XML properties are misconfigured. Check your layout file for these attributes on actventryDivName and actvProductType:

<com.yourpackage.CustomAutoCompleteTextView
    android:id="@+id/actventry_div_name"
    ...
    android:focusable="true"
    android:focusableInTouchMode="true"/>

Also, check if any parent view groups have android:descendantFocusability="blocksDescendants"—this will prevent child views from gaining focus, so you'll need to change it to beforeDescendants or afterDescendants if that's the case.

3. Inspect your CustomAutoCompleteTextView implementation

Since this is a custom view, it's possible that internal overrides are blocking the focus change callback. Look for an onFocusChanged method in your CustomAutoCompleteTextView class—if you've overridden it, you must call the superclass method to trigger the external listener:

@Override
protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
    // This line is critical—without it, external listeners won't get notified
    super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
    // Your custom focus logic here (if any)
}

If you're handling focus internally without calling super, that's almost certainly why the external listeners aren't firing.

4. Test focus switching properly

Make sure you're actually triggering a focus change correctly during testing:

  • Tap the problematic view to give it focus (you should see a cursor or highlight)
  • Tap another focusable view (like a button or the first actventry control) to force it to lose focus
  • Add debug logs to your onFocusChange method to confirm if the callback is running at all:
    Log.d("FocusDebug", "View ID: " + v.getId() + " | Has focus: " + hasFocus);
    

Example working implementation

Here's a simplified, complete example of how your focus listener logic should look to ensure all three controls work:

public class YourActivity extends AppCompatActivity implements View.OnFocusChangeListener {

    private CustomAutoCompleteTextView actventry, actventryDivName, actvProductType;
    private List<String> vendorList, divNameList, productTypeList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_layout);

        // Initialize views
        actventry = findViewById(R.id.actventry);
        actventryDivName = findViewById(R.id.actventry_div_name);
        actvProductType = findViewById(R.id.actv_product_type);

        // Attach listeners
        actventry.setOnFocusChangeListener(this);
        actventryDivName.setOnFocusChangeListener(this);
        actvProductType.setOnFocusChangeListener(this);

        // Set up data sources
        vendorList = Arrays.asList("Vendor A", "Vendor B", "Vendor C");
        divNameList = Arrays.asList("Division 1", "Division 2", "Division 3");
        productTypeList = Arrays.asList("Electronics", "Apparel", "Home Goods");

        // Attach adapters
        actventry.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, vendorList));
        actventryDivName.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, divNameList));
        actvProductType.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, productTypeList));
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (!hasFocus) {
            CustomAutoCompleteTextView currentActv = (CustomAutoCompleteTextView) v;
            String input = currentActv.getText().toString().trim();
            List<String> targetList = null;

            // Match view to its data source
            if (v.getId() == R.id.actventry) {
                targetList = vendorList;
            } else if (v.getId() == R.id.actventry_div_name) {
                targetList = divNameList;
            } else if (v.getId() == R.id.actv_product_type) {
                targetList = productTypeList;
            }

            // Clear input if it's not in the data list
            if (targetList != null && !targetList.contains(input)) {
                currentActv.setText("");
            }
        }
    }
}

Start with these checks—chances are one of these steps will fix the issue with your focus listeners!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:37:59