2016-03-08 89 views
1

我有兩個EditText在我的actvity_main,點擊它我想禁用android的軟鍵盤。爲了實現這個功能,我正在關注this link隱藏軟鍵盤爲一個EditText工作,但不能正常工作其他

兩個EditText上有以下ID分配給它

firstText and secondText 

這是我同時應用於EDITTEXT

firstText.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      v.onTouchEvent(event); 
      InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); 
      if (imm != null) { 
       imm.hideSoftInputFromWindow(v.getWindowToken(), 0); 
      }     
      return true; 
     } 
    }); 

對於secondText只有名稱變化的代碼。對於兩個EditText軟鍵盤都不顯示。這正是我想要的,但我也想要光標。在這種情況下,firstText不顯示鍵盤,但具有光標。對於secondText,我無法保留遊標。

我試着在條件和setOnTouchListener內打印值。他們都完美無缺。

有人可以幫助我在哪裏出錯嗎?

謝謝!提前

回答

1

您可以使用下面的代碼來顯示和隱藏鍵盤,它適用於我。

public static void showKeyboard(Activity activity) { 
     if (activity != null) { 
      activity.getWindow() 
        .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 
     } 
    } 

    public static void hideKeyboard(Activity activity) { 
     if (activity != null) { 
      activity.getWindow() 
        .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
     } 
    } 

希望它對您有幫助。

+0

感謝您的回答,但我解決了這個問題。顯然,我已經宣佈了在secondText上隱藏軟鍵盤的更多條件。 –