简体   繁体   English

Android-Edittext软键盘

[英]Android - Edittext soft keyboard

I've a ListView with an EditText ; 我有一个带有EditTextListView when I touch the EditText , the soft keyboard will appear and I've entered some text, it's fine. 当我触摸EditText ,将出现软键盘,并且我输入了一些文本,这很好。 But after entering the text, press backspace button (not back button) and the activity will finish. 但是在输入文本后,按退格按钮(而不是后退按钮),活动将结束。

Why does it happen ? 为什么会发生? What went wrong here? 这里出了什么问题? How can i solve this? 我该如何解决? Please someone help me to find out this riddle. 请有人帮我找出这个谜语。

In xml. 在xml中。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <EditText 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minLines="15"
        android:textColor="#000000"
        android:background="@drawable/back"
        />
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Comments"
        android:textColor="#000000"
        android:visibility="gone"/>
    <TextView 
        android:id="@+id/notes_to_parents"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"/>

</LinearLayout>

In java. 在Java中。 This for loop is inside of Listview onScroll Action. 此for循环在Listview onScroll Action内部。

for (int i = 0; i < daily_dairy_2.getChildCount(); i++) 
                {
LinearLayout layout = (LinearLayout) daily_dairy_2.getChildAt(i);
final EditText notes_to     = (EditText) layout.getChildAt(0);

}

I know here i don't write any code for edittext. 我知道在这里我没有为edittext写任何代码。 How to hanle this edittext. 如何处理此edittext。

You can set OnKeyListener for you editText so you can detect any key press 您可以为editText设置OnKeyListener,以便可以检测到任何按键

editText.setOnKeyListener(new OnKeyListener() {                 
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            //You can identify which key pressed buy checking keyCode value with KeyEvent.KEYCODE_
             if(keyCode == KeyEvent.KEYCODE_DEL){  
                 //this is for backspace
                 }
        return false;       
            }
    });

this is code handle backspace Button 这是代码句柄退格按钮

Make your own implementation for the press of backspace. 按退格键制作自己的实现。

It's something like this 是这样的

public final static int CodeDelete   = -5;
public class MyKeyboard extends InputMethodService
implements OnKeyboardActionListener{
public void onKey(int primaryCode, int[] keyCodes) {    
InputConnection ic = getCurrentInputConnection();
 switch(primaryCode){
    case CodeDelete :
        ic.deleteSurroundingText(1, 0);
        break;

and in your keyboard's xml: 并在键盘的xml中:

        <Key android:codes="-5" android:keyIcon="@drawable/sym_keyboard_delete_dim">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM