Android之禁止Edittext弹出软键盘和光标正常显示并在光标处增删字符

来源:互联网 发布:淘宝买的人参 编辑:程序博客网 时间:2024/06/06 03:57

在开发中,当我们使用EditText输入框时,总会自动弹出系统自带的键盘,很多时候我们要根据需求自定义控件规范输入框的内容,这是就需要把自带键盘给屏蔽掉,并且为了灵活使用,我们总要在光标出实现增删。如下图:



网上有很多种屏蔽系统键盘的方法:

方法一:
  在 AndroidMainfest.xml中选择activity,设置windowSoftInputMode属性为 adjustUnspecified|stateHidden

< activity Android:name=".Main"     android:label="@string/app_name"     android:windowSoftInputMode="adjustUnspecified|stateHidden" />  


方法二:
  让 EditText失去焦点,使用EditText的clearFocus方法

EditText edit=(EditText)findViewById(R.id.edit);  edit.clearFocus();  



方法三:
  强制隐藏Android输入法窗口

EditText edit=(EditText)findViewById(R.id.edit);     InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);     imm.hideSoftInputFromWindow(edit.getWindowToken(),0);  


经过实现测试之后,你会发现都无法实现我们所需要的需求。


接下来我用一个小Demo演示实现一下另一种方法:

新建一个Android小Demo,在activity_main.xml中实现以下布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.itman.edittextdemo.MainActivity" >    <EditText        android:id="@+id/et_content"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:ems="10" >    </EditText>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <Button            android:id="@+id/bt_add_a"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="添加A" />        <Button            android:id="@+id/bt_add_b"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="添加B" />        <Button            android:id="@+id/bt_delete"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="删除" />        </LinearLayout>            </LinearLayout>



然后在MainActivity中实现以下代码:

public class MainActivity extends ActionBarActivity implements OnClickListener {private EditText et_content;private Button bt_add_a;private Button bt_add_b;private Button bt_delete;private StringBuilder stringBuilder = new StringBuilder();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);InitView();disableShowSoftInput();}private void InitView() {et_content = (EditText) findViewById(R.id.et_content);bt_add_a = (Button) findViewById(R.id.bt_add_a);bt_add_b = (Button) findViewById(R.id.bt_add_b);bt_delete = (Button) findViewById(R.id.bt_delete);bt_add_a.setOnClickListener(this);bt_add_b.setOnClickListener(this);bt_delete.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.bt_add_a:addInputRotateLetter("A");break;case R.id.bt_add_b:addInputRotateLetter("B");break;case R.id.bt_delete:deleteInputRotateLetter();break;}}/** * 在光标处增加字母 */private void addInputRotateLetter(String str) {//获取光标的位置int index = et_content.getSelectionStart();//在光标的位置插入字符stringBuilder = stringBuilder.insert(index, str);et_content.setText(stringBuilder.toString());//光标显示回原来的位置Selection.setSelection(et_content.getText(), index + 1);}/** * 在光标处删除字母 */private void deleteInputRotateLetter() {//获取光标的位置int index = et_content.getSelectionStart();if (index > 0) {  //判断是否是没有字符//在光标的位置删除字符stringBuilder = stringBuilder.delete(index - 1, index);et_content.setText(stringBuilder.toString());//光标显示回原来的位置Selection.setSelection(et_content.getText(), index - 1);}}/** * 禁止Edittext弹出软件盘,光标依然正常显示。 */private void disableShowSoftInput() {if (android.os.Build.VERSION.SDK_INT <= 10) {et_content.setInputType(InputType.TYPE_NULL);} else {Class<EditText> cls = EditText.class;Method method;try {method = cls.getMethod("setShowSoftInputOnFocus", boolean.class);method.setAccessible(true);method.invoke(et_content, false);} catch (Exception e) {}try {method = cls.getMethod("setSoftInputShownOnFocus",boolean.class);method.setAccessible(true);method.invoke(et_content, false);} catch (Exception e) {}}}}

界面非常简陋,不过没关系,我们主要实现功能。代码不多,注解非常详细,接下来运行看看:


实验证明是完全符合需求的!!!

阅读全文
0 0