How to set focus on a TextView when an Activity starts?

前端 未结 7 1487
我在风中等你
我在风中等你 2020-12-28 15:49

There are an EditText and a TextView in my Activity. I want to set focus on the TextView when the Activity starts. I used the following code:

    TextView my         


        
相关标签:
7条回答
  • 2020-12-28 16:07

    If anyone is still wondering, you can use the following in your layout xml to make a textview focusable in touch mode:

    android:focusableInTouchMode="true"
    
    0 讨论(0)
  • 2020-12-28 16:09

    Add the TextView Property in layout

    android:focusable="false"
    

    It's work for me.

    0 讨论(0)
  • 2020-12-28 16:10

    This is expected when you start the Activity using the touch screen. When in "touch mode," only a couple of Views (EditText and ListView) can receive the focus. If you start your application using the trackball you will see the focus on the TextView. What you are seeing is the correct and expected result.

    0 讨论(0)
  • 2020-12-28 16:17

    Use this:

    EditText mEditText = (EditText) findViewById(R.id.edit_text);
    TextView myTextView = (TextView) findViewById(R.id.my_text_vew);
    
    mEditText.setFocusable(false);  
    myTextView.setFocusable(true);
    

    This takes focus off of the EditText and puts it on the TextView.

    0 讨论(0)
  • 2020-12-28 16:17

    You can set focus to any widget or a TextView from the xml of the activity. Just add requestFocus before the TextView end.

    <AutoCompleteTextView
            android:id="@+id/autoCompleteTextView2"
            android:layout_width="470dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="90dp"
            android:layout_toEndOf="@+id/textView2"
            android:ems="10"
            android:textColor="#455A64"
            android:textColorHint="#455A64"
            android:textSize="27sp">
    
            <requestFocus />
        </AutoCompleteTextView>
    
    0 讨论(0)
  • 2020-12-28 16:18

    I came across with your question now. Maybe after 2 years you have found a solution. But I write it here for the others that will come across with this in the future. The only thing you have to do is to go to the AndroidManifest.xml, find the Activity in which you want this behaviour and add this android:windowSoftInputMode="stateHidden". So, at the start of your Activity no keyboard will be shown, until you touch the EditText object.

    0 讨论(0)
提交回复
热议问题