简体   繁体   English

setOnFocusChangeListener 在 Android 中如何工作?

[英]How does setOnFocusChangeListener work in Android?

I have designed a simple Android Application to test how setOnFocusChangeListener works.我设计了一个简单的 Android 应用程序来测试setOnFocusChangeListener工作原理。

The Java Code: Java 代码:

public class MainActivity extends AppCompatActivity {
    TextView tv;
    EditText et;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = findViewById(R.id.textView);
        et = findViewById(R.id.edittext);
        et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    tv.setText("Edit Text is focused!!");
                }
                else {
                    tv.setText("You stopped focussing on editText");
                }
            }
        });
    }
}

The XML Code: XML 代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Nothing happened"
        android:textSize="30sp"
        android:gravity="center"
        android:id="@+id/textView" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:hint="Write here!!!"
        android:id="@+id/edittext" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Click Me!!" />

</LinearLayout>

But when I click on the edit text to write something, Edit Text is focused.!但是当我点击编辑文本来写东西时,编辑文本是重点! and displayed on the text view.并显示在文本视图上。

After stopping the focus, the EditText is never displayed on screen, even when I click anywhere else on the screen or on a button.停止焦点后, EditText永远不会显示在屏幕上,即使我单击屏幕上的其他任何位置或按钮也是如此。 I would like to know when the else part of onFocusChange will be displayed?我想知道onFocusChangeelse部分什么时候会显示?

I guess it is cause the editText is never leaving the focus.我想这是因为 editText 永远不会离开焦点。

On the GUI editor or XML, there is a property named "focusOnTouch" which editText Views have on true by default that let you get the focus for the editText, but if there is no other view with that property on true too, no view could get the focus.在 GUI 编辑器或 XML 上,有一个名为“focusOnTouch”的属性,editText 视图默认设置为 true,可以让您获得 editText 的焦点,但如果没有其他视图将该属性也设置为 true,则没有视图可以获得焦点。

That property exists for every View, set it to true for each View you want to consider to get the focus when clicking, otherwise, the focus won't be passed to other views like your textView or button.该属性存在于每个视图中,将其设置为 true 对于您要考虑在单击时获得焦点的每个视图,否则,焦点不会传递给其他视图,例如 textView 或按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM