programing

Android:텍스트 편집을 강제로 수행하여 포커스를 제거하시겠습니까?

copysource 2023. 6. 4. 17:51
반응형

Android:텍스트 편집을 강제로 수행하여 포커스를 제거하시겠습니까?

텍스트 편집에서 포커스를 제거할 수 있습니다.예를 들어 키보드가 나타나고 사용자가 뒤로 단추를 눌러 숨기면 포커스와 커서가 사라집니다.어떻게 할 수 있습니까?

커서와 포커스를 다음과 같이 사라지게 할 수 있습니다.

edittext.clearFocus();

그러나 키보드 숨기기가 어려운 경우 탐지합니다.

다음에 추가할 수 있습니다.onCreate그리고 그것은 매번 키보드를 숨길 것입니다.Activity시작합니다.

다른 항목으로 포커스를 프로그래밍 방식으로 변경할 수도 있습니다.

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

더하다LinearLayout 앞에EditTextXML 파일에 저장합니다.

<LinearLayout 
    android:focusable="true"
    android:focusableInTouchMode="true" 
    android:clickable="true"
    android:layout_width="0px"
    android:layout_height="0px" />

또는 '편집' 전에 보기 위해 이 행을 추가하여 동일한 작업을 수행할 수 있습니다.'문자'.

<Button
    android:id="@+id/btnSearch"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:gravity="center"
    android:text="Quick Search"
    android:textColor="#fff"
    android:textSize="13sp"
    android:textStyle="bold" />

<EditText
    android:id="@+id/edtSearch"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="5dp"
    android:gravity="left"
    android:hint="Name"
    android:maxLines="1"
    android:singleLine="true"
    android:textColorHint="@color/blue"
    android:textSize="13sp"
    android:textStyle="bold" />

포커스를 제거하지만 포커스를 유지할 수 있습니다.

editText.setFocusableInTouchMode(false);
editText.setFocusable(false);
editText.setFocusableInTouchMode(true);
editText.setFocusable(true);

EditText는 포커스를 잃지만 새 터치 이벤트에서 포커스를 다시 얻을 수 있습니다.

이 두 속성을 상위 레이아웃에 추가합니다(예: 선형 레이아웃, 상대 레이아웃).

android:focusable="true"
android:focusableInTouchMode="true" 

그것은 속임수를 쓸 것입니다 :)

자동 포커스 제거 텍스트 편집 Android

나한테 효과가 있어요.

편집 링크에서 선형 레이아웃을 사용할 것을 제안하지만 단순 보기가 작동합니다.

<View
    android:id="@+id/focus_thief"
    android:layout_width="1dp"
    android:layout_height="1dp"
    android:focusable="true"
    android:focusableInTouchMode="true" />

다음 의 맨 (첫 번째 수 있는이 됨) )▁to▁then▁this것),(경th▁(▁is"될우▁focus▁layout▁ifiefto▁"able▁placed▁calls),clearFocus()효과가 있을 것입니다.

매니페스트 작업 섹션에 Android:windowSoftInputMode="stateAlwaysHidden"포함할 수도 있습니다.

이는 다음과 같습니다.

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

하지만 XML 방식으로.

참고로, 코드로 키보드를 숨길 수도 있습니다.

// hide virtual keyboard
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mYourEditText.getWindowToken(), 0);

활동이 시작될 때 키보드를 숨기려면...Create()에 다음 코드를 기록합니다.

InputMethodManager imm = (InputMethodManager)
getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);

포커스를 지우고 편집 텍스트에서 커서를 제거하려면...

editText.clearFocus();

editText.setCursorVisible(false);

당신의 관점에서 이것을 사용해 보세요. 그것은 저에게 효과가 있었습니다.

<View
    android:id="@+id/fucused"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="true"
    android:focusableInTouchMode="true"/>

에 레에추다니가합웃아부이를 합니다. 배치EditText 것이것android:focusableInTouchMode="true"

야 합니다를 제거해야 .<requestFocus/>

만약 당신이 그것을 사용하지 않고 여전히 같은 문제가 있다면.

사용자LinearLayout

android:focusable="true"
android:focusableInTouchMode="true"

도움이 되길 바랍니다.

SO에 대한 첫 답변이니 실수가 있더라도 너무 심하게 하지 마세요.:D

SO 주변에는 답변이 거의 없지만, 저는 이것이 저를 미치게 했기 때문에 저의 완전한 해결책을 게시하고 싶은 충동을 느낍니다.제가 여기저기서 잡았으니까 제가 모든 사람들에게 각각의 학점을 주지 않는다면 용서해 주세요...:)

(내 보기에는 요소가 너무 많아서 스팸을 보내고 싶지 않기 때문에 결과를 단순화하고 가능한 한 일반적으로 만들겠습니다...)

레이아웃에 대해 다음과 같이 정의된 EditText부모 보기가 부모에게 필요합니다.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
              android:orientation="vertical"
              android:layout_width="match_parent" 
              android:layout_height="match_parent"
              android:id="@+id/lytContainer"
              android:descendantFocusability="beforeDescendants"
              android:focusableInTouchMode="true">
<EditText android:layout_width="fill_parent" 
              android:layout_height="wrap_content" 
              android:id="@+id/etEditor"
              android:inputType="number"
              android:layout_gravity="center" 
              android:hint="@string/enter_your_text"
              android:textColor="@android:color/darker_gray" 
              android:textSize="12dp"
              android:textAlignment="center" 
              android:gravity="center" 
              android:clickable="true"/>
</LinearLayout>

그래서, 저는 여기에 몇 가지가 필요했습니다.내 편집 텍스트에 대한 자리 표시자가 필요했습니다. 즉, -

Android:model="model"

또한.

android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"

설정을 사용하면 EditText가 활동 입력에 초점을 맞추지 않고 나중에 활동 자체에 초점을 맞추게 되므로 TouchListener를 설정하여 EditText에서 포커스를 빼낼 수 있습니다.

이제 활동에서:

package com.at.keyboardhide;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;

public class MainActivity extends Activity implements OnTouchListener{
private EditText getEditText;
private LinearLayout getLinearLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

    setContentView(R.layout.keyboardmain);
    getEditText = (EditText)findViewById(R.id.etEditor);
    getLinearLayout = (LinearLayout)findViewById(R.id.lytContainer);
    getLinearLayout.setOnTouchListener(this);

    getEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                Log.d("EDTA", "text was entered.");
                getEditText.clearFocus();
                imm.hideSoftInputFromWindow(barcodeNo.getWindowToken(), 0);
                return true;
            }
            return false;
        }
    });
}
@Override
public boolean onTouch(View v, MotionEvent event) {
    if(v==getLinearLayout){
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getEditText.getWindowToken(), 0);
        getEditText.clearFocus();
        return true;
        }
    return false;
    }
}

이 질문 페이지에서 찾은 비트에 대한 답변 중 일부와 블로그에서 찾은 활동 솔루션에 대한 부분입니다.제가 놓친 나머지 부분은 부모 보기를 위해 세트OnEditorActionListener 내부와 TouchLister에 모두 추가한 EditText에 대한 초점을 지우는 것이었습니다.

이것이 누군가에게 도움이 되고 그들의 시간을 절약하기를 바랍니다.:)

건배, Z.

설명에서 텍스트 편집 대신 다른 보기에 초점을 맞출 수 있는지 여부를 묻습니다.네, 가능합니다.사용하다.requestFocus()처음에 초점을 맞추려는 뷰의 메서드(Create() 메서드)

또한 다른 보기에 초점을 맞추면 일부 코드가 삭제됩니다.(예: 키보드 숨기기 코드)

저도 같은 문제가 있었습니다.그것은 저를 미친 것 이상으로 만들었습니다.

ScrollView를 사용한 확장 대화상자에서 테이블 레이아웃과 SeekBar 및 EditText가 포함된 확장 선형 레이아웃을 사용했습니다.

첫 번째 편집 텍스트는 대화 상자를 표시하고 키보드를 통해 텍스트 편집을 마친 후 항상 자동 포커스를 가집니다. 편집 텍스트는 여전히 포커스를 유지하고 키보드는 여전히 볼 수 있습니다.

저는 이 스레드의 거의 모든 솔루션을 시도했지만 아무 것도 제게 효과가 없었습니다.

여기 간단한 해결책이 있습니다. (텍스트 = 텍스트 편집)

text.setOnEditorActionListener( new OnEditorActionListener( ){
    public boolean onEditorAction( TextView v, int actionId, KeyEvent event ){
        if( (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) ||
            (actionId == EditorInfo.IME_ACTION_DONE) ){
            text.clearFocus( );
            InputMethodManager iMgr = null;
            iMgr = (InputMethodManager)mContext.getSystemService( Context.INPUT_METHOD_SERVICE );
            iMgr.hideSoftInputFromWindow( text.getWindowToken(), 0 );
        }
        return true;
    }
});

그런데 저는 이 문제를 해결하기 위해 다음 스니펫을 사용하지 않았습니다.

//setFocusableInTouchMode( true )
//setFocusable( true )
//setDescendantFocusability( ViewGroup.FOCUS_BEFORE_DESCENDANTS )

그리고 가로와 세로가 1dp인 뷰와 같은 스페이서 아이템은 사용하지 않았습니다.

누군가에게 도움이 되기를 바랍니다.d

editText.setFocusableInTouchMode(true)

EditText사용자가 터치할 때 초점을 맞출 수 있습니다. 레이아웃 등되면 "" (activity, dialog 등)"이 표시됩니다.EditText레이아웃의 첫 번째 보기임에도 불구하고 자동으로 포커스를 얻지 못합니다.

다음을 포함할 수도 있습니다.android:windowSoftInputMode="stateAlwaysHidden"당신의 매니페스트 액션 섹션에서.

이는 다음과 같습니다.

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

상위 요소의 rodroid:descendantFocusability 속성을 설정하면 요소에 대한 포커스를 피할 수 있습니다.

다음은 예입니다.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/search__scroller"
android:descendantFocusability="blocksDescendants"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ScrollView>

여기서 "blocksDescendants"로 설정된 Android:descendantFocusability 특성은 하위 요소에 대한 포커스를 차단합니다.

자세한 내용은 여기에서 확인할 수 있습니다.

check your xml file
 <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="14sp" >

            **<requestFocus />**
 </EditText>


//Remove  **<requestFocus />** from xml

언급URL : https://stackoverflow.com/questions/5056734/android-force-edittext-to-remove-focus

반응형