IT story

레이아웃을 생성하고 표시 할 때 뷰에 초점을 맞추는 방법은 무엇입니까?

hot-time 2020. 9. 7. 21:24
반응형

레이아웃을 생성하고 표시 할 때 뷰에 초점을 맞추는 방법은 무엇입니까?


현재, Buttona TextViewEditText. 레이아웃이 표시되면 포커스가 자동으로에 놓 EditText이고 키보드가 Android 휴대폰에 표시되도록 트리거합니다. 내가 원하는 것이 아닙니다. TextView레이아웃이 표시 될 때 포커스를 설정 하거나 아무것도 설정 하지 않을 수있는 방법이 있습니까?


포커스 설정 : 프레임 워크는 사용자 입력에 대한 응답으로 이동 포커스를 처리합니다. 특정보기에 초점을 맞추려면 requestFocus ()를 호출하십시오.


이것은 작동합니다 :

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

포커스를 설정하려면 핸들러를 사용하여 requestFocus ()를 지연합니다.

private Handler mHandler= new Handler();

public class HelloAndroid extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);

     LinearLayout mainVw = (LinearLayout) findViewById(R.id.main_layout);

     LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( 
           LinearLayout.LayoutParams.FILL_PARENT,
           LinearLayout.LayoutParams.WRAP_CONTENT);

     EditText edit = new EditText(this);
     edit.setLayoutParams(params);
     mainVw.addView(edit);

     TextView titleTv = new TextView(this);
     titleTv.setText("test");
     titleTv.setLayoutParams(params);
     mainVw.addView(titleTv);

     mHandler.post(
       new Runnable() 
       {
          public void run() 
          {
            titleTv.requestFocus();
          } 
       }
     );
   }
}

키보드를 숨기면됩니다. 이 같은:

InputMethodManager inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

다음을 추가해야합니다.

android:focusableInTouchMode="true"

세트

 android:focusable="true"

당신의 <EditText/>


시험

comp.requestFocusInWindow();

Set these lines to OnResume as well and make sure if focusableInTouch is set to true while you initialize your controls

<controlName>.requestFocus();

<controlName>.requestFocusFromTouch();

None of the answers above works for me. The only (let's say) solution has been to change the first TextView in a disabled EditText that receives focus and then add

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

in the onCreate callback to prevent keyboard to be shown. Now my first EditText looks like a TextView but can get the initial focus, finally.


to change the focus make the textView in xml focusable

<TextView
            **android:focusable="true"**
            android:id="@+id/tv_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

and in java in on create

textView.requestFocus();

or simply hide the keyboard

public void hideKeyBoard(Activity act) {
    act.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    InputMethodManager imm = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
}

i think a text view is not focusable. Try to set the focus on a button for example, or to set the property focusable to true.


you can add an edit text of size "0 dip" as the first control in ur xml, so, that will get the focus on render.(make sure its focusable and all...)


The last suggestion is the correct solution. Just to repeat, first set android:focusable="true" in the layout xml file, then requestFocus() on the view in your code.


Set android:focusable="true" in the layout xml file, then requestFocus() on the view in your code

NO, it doesn't work here.


You can start by adding android:windowSoftInputMode to your activity in AndroidManifest.xml file.

<activity android:name="YourActivity"
          android:windowSoftInputMode="stateHidden" />

This will make the keyboard to not show, but EditText is still got focus. To solve that, you can set android:focusableInTouchmode and android:focusable to true on your root view.

<LinearLayout android:orientation="vertical"
              android:focusable="true"
              android:focusableInTouchMode="true"
              ...
              >
    <EditText
         ...
       />
    <TextView
         ...
       />
    <Button
         ...
       />
</LinearLayout>

The code above will make sure that RelativeLayout is getting focus instead of EditText


Focus is for selecting UI components when you are using something besides touch (ie, a d-pad, a keyboard, etc.). Any view can receive focus, though some are not focusable by default. (You can make a view focusable with setFocusable(true) and force it to be focused with requestFocus().)

However, it is important to note that when you are in touch mode, focus is disabled. So if you are using your fingers, changing the focus programmatically doesn't do anything. The exception to this is for views that receive input from an input editor. An EditText is such an example. For this special situation setFocusableInTouchMode(true) is used to let the soft keyboard know where to send input. An EditText has this setting by default. The soft keyboard will automatically pop up.

If you don't want the soft keyboard popping up automatically then you can temporarily suppress it as @abeljus noted:

InputMethodManager inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

When a user clicks on the EditText, it should still show the keyboard, though.

Further reading:

참고URL : https://stackoverflow.com/questions/2150656/how-to-set-focus-on-a-view-when-a-layout-is-created-and-displayed

반응형