简体   繁体   中英

Android - Edittext soft keyboard

I've a ListView with an EditText ; when I touch the EditText , the soft keyboard will appear and I've entered some text, it's fine. 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 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. This for loop is inside of 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. How to hanle this edittext.

You can set OnKeyListener for you editText so you can detect any key press

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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