Android - 如何获取当前"focused"对象?

Android - How to get the currently "focused" object?

我想让 Button 识别我的 TextViews 中的哪个获得了当前焦点。之后 Button 需要将更改应用到焦点 TextView.

    TextView textView11 = (TextView) findViewById(R.id.frame1_1);

    textView11.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            textView11.setBackgroundColor(Color.LTGRAY);
            textView11.setFocusableInTouchMode(true);
            textView11.requestFocus();
        }
    });

    MaterialButton pb1 = (MaterialButton) findViewById(R.id.pin_button_1);
    pb1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //1. get the focused textview or scan for the focused textView
            
            //2. apply button function to the focused textView

        }

    });

那我怎么在这里识别

//1. get the focused textview or scan for the focused textView

当前哪个TextView有焦点? 我认为它可能是 possible 一些 help variable 但也许有 os 另一种方式,因为那样我仍然需要一个 onClickListener 为每个 22 TextView

你可以简单地将这些TextView放在同一个ViewGroup中,然后像这样遍历ViewGroup的子视图:

for (child in container.children){
    if (child.isFocused){
        //do your jobs here
    }
}

未经测试但应该有效

private TextView hasFocusTextView;
private View.OnFocusChangeListener onFocusChangedListener = new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus)
            hasFocusTextView = (TextView) v;
    }
};

..

TextView textView11 = (TextView) findViewById(R.id.frame1_1);
//Use same listener on all textview you want to include with the focus logic
textView11.setOnFocusChangeListener(onFocusChangedListener); 

pb1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            hasFocusTextView.setText("Changing text of the focused TextView");
        }

});

最好的方法是使用一个变量来存储当前获得焦点的 TextView。

TextView currentTextView = null;

void initView() {
    TextView textView11 = (TextView) findViewById(R.id.frame1_1);

    textView11.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            textView11.setBackgroundColor(Color.LTGRAY);
            textView11.setFocusableInTouchMode(true);
            textView11.requestFocus();

            // Set the currentTextView to the currently focused
            // this must be used in every focusable textview
            currentTextView = (TextView) view;
        }
    });

    MaterialButton pb1 = (MaterialButton) findViewById(R.id.pin_button_1);
    pb1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //1. get the focused textview or scan for the focused textView
            
            // focused textview is currentTextView

            //2. apply button function to the focused textView

            // Edit: Check for nullability
            if (currentTextView != null) {
                // use the variable currentTextView
            }
        }

    });
}