2016-12-31 124 views
2

單擊EditText時,會出現軟鍵盤。我不想讓鍵盤出現在我點擊EditText時,但是,我想要得到EditText的力量。我想在沒有鍵盤的情況下進行編輯。如何在使用EditText時使軟鍵盤無效

editText.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     InputMethodManager imm = (InputMethodManager) 
     getSystemService(Context.INPUT_METHOD_SERVICE); 
     imm.hideSoftInputFromWindow(view.getWindowToken(), 
     InputMethodManager.HIDE_NOT_ALWAYS); 
    } 
}); 

在此代碼中,當我點擊一次時,鍵盤不會出現。但是當我多次點擊鍵盤時會出現鍵盤。

我該如何改進? 請告訴我該怎麼做。謝謝。

+0

請解釋一下你想,因爲你可以使EDITTEXT無法點擊,但再有沒有EDITTEXT的意義因此而不是使用EditText上你可以使用TextView的究竟是什麼。請解釋一下你的輸出 –

+0

@ user7359831試試我的答案:) –

+0

我想用我的鍵盤。我正在製作計算器,所以我不需要鍵盤。 – user7359831

回答

0

重點:

onCheckIsTextEditor()檢查稱爲視圖是一個文本編輯器,在這種情況下,它將使意義它自動顯示軟輸入窗口。如果此視圖是文本編輯器,則返回true。所以讓我們改變它!然後,鍵盤沒有出現:)

創建自己的類

import android.content.Context; 
import android.util.AttributeSet; 
import android.widget.EditText; 


public class NoImeEditText extends EditText { 
    public NoImeEditText(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 
    @Override 
    public boolean onCheckIsTextEditor() { 
     return false; 
    } 
} 

設定這樣的XML

<your.package.name.NoImeEditText 
     android:textSize="17dp" 
     android:textColor="#FFF" 
     android:id="@+id/edit_query" 
     android:layout_width="match_parent" 
     android:layout_height="60dp" /> 

稱之爲

editText = (NoImeEditText) findViewById(R.id.edit_query); 

檢查重點

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
      @Override 
      public void onFocusChange(View v, boolean hasFocus) { 
       if(hasFocus){ 
         // show your custom key pad and do your work 
        Toast.makeText(getApplicationContext(), "got the focus", Toast.LENGTH_LONG).show(); 
       }else { 
        Toast.makeText(getApplicationContext(), "lost the focus", Toast.LENGTH_LONG).show(); 
       } 
      } 
     }); 
+0

謝謝你的回答。當我在同一時間點擊很多時,鍵盤會出現。 – user7359831

+0

@ user7359831現在試試 –

+0

謝謝。有一些條件。輸入一些字母並點擊它們。它不起作用。另一方面,它在editText中沒有字母時有效。 – user7359831

相關問題