2011-09-29 43 views
30

Tôi có một chỉnh sửa văn bản:Disable tự động lấy nét trên chỉnh sửa văn bản

<LinearLayout android:id="@+id/linearLayout7" android:layout_width="match_parent" android:layout_height="wrap_content"> 
     <EditText android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight="1" android:id="@+id/editText1" android:text="3"> 
      <requestFocus></requestFocus> 
     </EditText> 
     <Button android:text="Button" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/button2"></Button> 
    </LinearLayout> 

và sau đó một số nội dung khác bên dưới

Vấn đề tôi đang gặp là đúng khi ứng dụng bắt đầu tập trung vào các đầu vào, tôi không muốn tập trung vào đầu vào ngay khi ứng dụng bắt đầu.

tôi đã cố gắng loại bỏ các điều

<requestFocus></requestFocus> 

và nó đã không làm bất cứ điều gì.

+0

Set android: đặt tiêu điểm = "false". Nó sẽ không tập trung vào EditText –

+1

của bạn mà sẽ làm cho un-facusable mãi mãi? bây giờ ngay cả khi tôi nhấp vào trường văn bản, nó sẽ không tập trung. Tôi muốn tập trung khi được nhấp, không tự động khi ứng dụng bắt đầu – Saad

+1

trước hết bạn có thể bắt đầu bằng cách nhận id nút và đặt tiêu điểm vào nút trong mã của bạn .... – Hanry

Trả lời

88

Thêm các phần tử android:focusable="true"android:focusableInTouchMode="true" trong bố cục gốc của EditText như sau;

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/linearLayout7" android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:focusable="true" android:focusableInTouchMode="true"> 

Tôi nghĩ, nó sẽ giúp bạn.

+7

không, nó không làm bất cứ điều gì – Saad

+1

@Saad: Nó hoạt động. Tôi đã thử nghiệm nó trước khi đăng nó ở đây. Giữ nguyên tố ở đầu tệp XML. BTW, cấp API nào (hoặc phiên bản Android) mà bạn đang phát triển? – Mudassir

+1

tốt. Bạn có thể giải thích những gì android: focusable và android: focusableInTouchMode được sử dụng cho (i có nghĩa là sự khác biệt và cách sử dụng). – Anoop

3

KHÔNG LÀM việc khác ... chỉ cần thêm android: windowSoftInputMode = "stateAlwaysHidden" vào thẻ hoạt động của tệp Manifest.xml.

+0

Hãy giải thích điều này làm gì và nó có thể giúp OP như thế nào. Các đề xuất không có lời giải thích không giúp ích gì nhiều, ngay cả khi bản sửa lỗi có thể hoạt động. – SuperBiasedMan

+0

https://developer.android.com/guide/topics/manifest/activity-element.html#wsoft –

+0

Tham khảo .. liên kết ở trên ... –

1
<EditText 
     android:id="@+id/input" 
     android:layout_width="0dp" 
     android:layout_height="48dp" 
     android:focusable="false" 
     android:focusableInTouchMode="false" /> 

detailInputView = (EditText) debugToolsView.findViewById(R.id.input); 
    detailInputView.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      detailInputView.setFocusable(true); 
      detailInputView.setFocusableInTouchMode(true); 
      return false; 
     } 
    }); 
+0

Nhưng điều này cũng không giúp lấy nét từ trường chỉnh sửa văn bản, nó chỉ thay đổi cách tải phần tử trên thiết bị, mặc dù nó hoạt động – HSL

0

Chỉ cần thêm dòng sau vào chính thẻ view layout xml của bạn:

android:focusableInTouchMode="true" 
0

Hei Lee,

Có một số cách để làm điều này, Mudassir có đưa ra một cách để bạn Để làm việc đó.

Nếu ý tưởng không làm việc, sau đó làm chỉ đơn giản thing-

Trong AndroidManifest>Activity thêm mã đơn giản này của bạn:

android:windowSoftInputMode="stateHidden" 

Không có gì cần phải làm trong EditText trong XML, tất cả sẽ được làm việc và không tập trung sẽ xuất hiện.

Nhìn cuộc sống thực mã ví dụ:

<activity 
     android:name=".MainActivity" 
     android:windowSoftInputMode="stateHidden" 
     android:label="@string/app_name" 
     > 

     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
</activity> 
0
<LinearLayout 
    android:focusable="true" 
    android:focusableInTouchMode="true" 
    android:layout_width="0px" 
    android:layout_height="0px" /> 

<EditText android:text="" 
    android:id="@+id/EditText01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:hint="@string/hint"> 
</EditText> 

Không khác mã hóa cần thiết, đó là một giải pháp đơn giản và rõ ràng.

2

Nếu bạn muốn xóa tập trung mặc định trên các chỉnh sửa văn bản sau đó sử dụng 2 thuộc tính sau

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

bên phụ huynh tuyến tính bố trí.

ví dụ:

<LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:focusable="false" 
      android:focusableInTouchMode="true" 
      android:orientation="vertical"> 


    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="email id" 
     android:inputType="textEmailAddress" 
     android:maxLines="1" 
    /> 

</LinearLayout> 

Nếu bạn muốn ẩn bàn phím trên tạo hoạt động sử dụng các thuộc tính bên trong manifest thẻ hoạt động tập tin ví dụ:

<activity 
    android:configChanges="screenSize|orientation|keyboardHidden" 
    android:screenOrientation="portrait" 
    android:name=".activities.LoginActivity" 
    android:windowSoftInputMode="stateHidden|adjustResize"/> 

Nếu bạn muốn để ẩn bàn phím khi nhấp vào nút hoặc trên một số sự kiện xảy ra, hãy sử dụng mã sau

public void onClick(View v) { 
       try { 
        //InputMethodManager is used to hide the virtual keyboard from the user after finishing the user input 
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
        if (imm.isAcceptingText()) { 
         imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); 
        } 
       } catch (NullPointerException e) { 
        Log.e("Exception", e.getMessage() + ">>"); 
       } 
        } 
       } catch (NullPointerException e) { 
        Log.e("Exception", e.getMessage() + ">>"); 
       } 

Nếu bạn muốn cất cánh trọng tâm từ các lĩnh vực chỉnh sửa văn bản sau khi rời khỏi hoạt động

@Override 
    protected void onResume() { 
     super.onResume(); 
     mEmail.clearFocus(); 
     mPassword.clearFocus(); 
} 

Và cuối cùng Nếu bạn muốn xóa các dữ liệu trong các lĩnh vực chỉnh sửa văn bản trên trình việc sử dụng hình thức

@Override 
    protected void onResume() { 
     super.onResume(); 
     mEmail.getText().clear(); 
     mPassword.getText().clear(); 
    } 
1

Bạn có thể sử dụng cửa sổ chế độ đầu vào mềm ẩn cho ẩn bàn phím và android:focusable="false"android:focusableInTouchMode="true"

<activity 
    android:name=".MainActivity" 
    android:windowSoftInputMode="stateHidden" 
    android:label="@string/app_name" 
    > 

    <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 
     <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
</activity> 
Các vấn đề liên quan