Android EditText - Delete the last letter with a button -
i have buttons writes , b edittext. if there in edittext how can delete last letters "del" button? layout:
<relativelayout 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" tools:context=".mainactivity" > <button android:id="@+id/buttonb" android:layout_width="100dp" android:layout_height="100dp" android:layout_alignparentleft="true" android:layout_below="@+id/buttona" android:text="@string/buttonb" android:textsize="50sp" android:textstyle="bold" /> <button android:id="@+id/buttona" android:layout_width="100dp" android:layout_height="100dp" android:layout_alignparentleft="true" android:layout_centervertical="true" android:text="@string/buttona" android:textsize="50sp" android:textstyle="bold" /> <button android:id="@+id/buttondel" android:layout_width="100dp" android:layout_height="100dp" android:layout_alignparentright="true" android:layout_centervertical="true" android:text="@string/buttondel" android:textsize="50sp" android:textstyle="bold" /> <edittext android:id="@+id/edittext" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:ems="58" android:textsize="20sp" android:textstyle="bold" android:inputtype="text" > <requestfocus /> </edittext> </relativelayout>
and java:
package com.koostamas.keyboard; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.app.activity; public class mainactivity extends activity implements onclicklistener { button buttona, buttonb; edittext edittext; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); edittext = (edittext) findviewbyid(r.id.edittext); addlisteneronbutton(); } public void addlisteneronbutton() { buttona = (button) findviewbyid(r.id.buttona); buttona.setonclicklistener(this); buttona.gettext(); buttonb = (button) findviewbyid(r.id.buttonb); buttonb.setonclicklistener(this); buttonb.gettext(); } public void onclick(view v) { button buttona = (button)v; edittext.settext(edittext.gettext().tostring()+buttona.gettext().tostring()); } public void onclick1(view v) { button buttonb = (button)v; edittext.settext(edittext.gettext().tostring()+buttonb.gettext().tostring()); } }
so want make it. please me! in advance.
yes can create onclicklistener , text edit text , delete last character.
button delete = (button) findviewbyid(r.id.buttondel); if( delete != null ) { delete.setonclicklistener(new onclicklistener() { @override public void onclick() { string textstring = edittext.gettext().tostring(); if( textstring.length() > 0 ) { edittext.settext(textstring.substring(0, textstring.length() - 1 )); edittext.setselection(edittext.gettext().length());//position cursor @ end of line } } }); }
edit: don't forget check string length greater 0 before doing in event or b hasn't been pressed when user hits delete.
Comments
Post a Comment