2016-12-16 129 views
0

我想一個很簡單的用戶界面,有一個EditTextImageView。當我點擊EditText時,我想接受來自用戶的輸入。現在,當我點擊ImageView(鍵盤仍然打開時)時,我想禁用EditText並同時阻止軟鍵盤關閉。但是setEnabled(false)會自動關閉軟鍵盤。Android的 - 防止Softkeyboard從EditText上後,關閉已被禁用

這裏是我的胡亞蓉:

public class SecondActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_second); 
     final EditText editText = (EditText) findViewById(R.id.edit_text); 
     final ImageView imageView = (ImageView) findViewById(R.id.image_view); 
     imageView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       editText.setEnabled(false); 
      } 
     }); 
    } 
} 

這裏是UI:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.pearson.android.test.stackoverflow_efficient_data_construction_recyclerview.SecondActivity" 
    tools:showIn="@layout/activity_second"> 

    <EditText 
     android:id="@+id/edit_text" 
     android:layout_width="300dp" 
     android:layout_height="wrap_content" /> 

    <ImageView 
     android:id="@+id/image_view" 
     android:layout_alignParentRight="true" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:background="@android:color/holo_red_dark"/> 

</RelativeLayout> 

我試圖保持softkeyboard打開,像這樣:

InputMethodManager inputManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); 
     inputManager.toggleSoftInput (InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY); 

,但有一個很明顯的閃爍(好像舊的鍵盤關閉並被新的鍵盤取代)。

我也試過沒有禁用的EditText(我不應該能夠輸入點擊圖片後的任何文本),並試圖操縱focasability代替,但沒有成功。

任何方向或解決方案將不勝感激。

+0

你爲什麼要保留的軟鍵盤打開,即使用戶不應該能夠輸入到它?這是糟糕的設計,用戶不知道發生了什麼。 –

回答

0

試試這個代碼隱藏按鍵板

imageView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       hideKeyboard(getContext()); 
      } 
     }); 
public static void hideKeyboard(Context ctx) { 
      InputMethodManager inputManager = (InputMethodManager) ctx 
        .getSystemService(Context.INPUT_METHOD_SERVICE); 
      View v = ((Activity) ctx).getCurrentFocus(); 
      if (v == null) 
       return; 

      inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0); 
     } 
+0

我不想隱藏鍵盤,我想要做的完全相反,保持甚至禁用的EditText – user1841702

+0

InputMethodManager IMM =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)後打開; imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0); – prsandroid

+0

嘗試按鈕點擊上面的代碼 – prsandroid