开发者

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

开发者 https://www.devze.com 2023-01-12 00:09 出处:网络
There are an EditText and a TextVi开发者_C百科ew in my Activity. I want to set focus on the TextView when the Activity starts. I used the following code:

There are an EditText and a TextVi开发者_C百科ew in my Activity. I want to set focus on the TextView when the Activity starts. I used the following code:

    TextView myTextView = (TextView) findViewById(R.id.my_text_vew);
    myTextView.setFocusable(true);
    myTextView.setOnClickListener(this);
    myTextView.requestFocus();

But the code doesn't work. The EditText always receives focus when the activity starts.

Can anyone help?

Thanks.


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.


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.


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"


In order to request focus to TextView you need to set focusable="true" and focusableInTouchMode="true" as follows:

 <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Heading"
            android:textSize="20sp"
            android:textStyle="bold"
            android:focusable="true"
            android:focusableInTouchMode="true">
                <requestFocus />
     </TextView>


Add the TextView Property in layout

android:focusable="false"

It's work for me.


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消