2016-04-01 33 views
2

我想使用光标移动和可选文本但没有键盘(如计算器应用程序)制作TextEdit。 我试过了一个解决方案,它在出现后隐藏了键盘。但是这非常缓慢,并且在消失之前它显示了一会儿。带光标但没有键盘的文本编辑器

有没有更好的解决方案(android 5.0.1及以上版本)?

android:windowSoftInputMode="stateHidden"(内部清单活动标签)没有工作

EditText et = (EditText) findViewById(R.id.editText); 
et.setInputType(InputType.TYPE_NULL); 
et.setCursorVisible(true); 
et.setTextIsSelectable(true); 

无论是。

在此先感谢

+0

为什么你想要光标,如果你不想输入任何数据? –

+0

@SagarNayak我想要一些按钮插入文本光标所在! –

回答

1

尝试在您的活动中添加以下代码。

android:windowSoftInputMode="stateHidden" 

一样,

<activity android:name=".MainActivity" 
      android:windowSoftInputMode="stateHidden" /> 

编辑

<FrameLayout 
    android:layout_width="0dp" 
    android:layout_height="0dp" 
    android:background="@android:color/transparent" 
    android:focusable="true" 
    android:focusableInTouchMode="true"> 
</FrameLayout> 

<EditText 
    android:id="@+id/searchAutoCompleteTextView_feed" 
    android:layout_width="200dp" 
    android:layout_height="wrap_content" 
    android:inputType="text" /> 

使用FrameLayout的要求重点。这不会增加重点EditText

+0

这很奇怪,但添加后仍然显示键盘! –

+0

@EbrahimTahernejad请检查我编辑的答案。 –

+0

对不起这个问题没有关系。但我编辑了我的清单xml,并且更改不会出现在应用程序中。我该怎么办 ? –

1

我用这个方法来隐藏键盘在我的Utils类:

public static void hideKeyboard(@NonNull Activity activity) { 
    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); 
    View f = activity.getCurrentFocus(); 
    if (null != f && null != f.getWindowToken() && EditText.class.isAssignableFrom(f.getClass())) 
     imm.hideSoftInputFromWindow(f.getWindowToken(), 0); 
    else 
     activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
} 
+0

我该如何使用它? –

+0

在你的utils类或类似的地方声明它,并调用方法: Class.hideKeyboard(getActivity()); (在片段中) 或 Class.hideKeyboard(this); (在Activity中) – dpulgarin

1

清单档案中的添加

android:windowSoftInputMode="stateAlwaysHidden" 

为您的活动。

+0

我记得这个,之前用过它。但现在如果我编译并运行在我的galaxy s6上,即使在我的activity标签中设置了该属性,键盘也会显示出来。我无法理解为什么! –

+0

我正在使用相同的设备并在所有设备上工作 – Piyush