remove 1 letter for a edittext

Put your problem here if it does not fit any of the other categories.

remove 1 letter for a edittext

Postby simondid » Tue Feb 22, 2011 5:38 pm

hey

so i need to make a button that removes 1 letter from a edittext but i can't find any why to due this pleas help
simondid
Developer
Developer
 
Posts: 34
Joined: Mon Jan 31, 2011 3:45 pm

Top

Re: remove 1 letter for a edittext

Postby blundell » Tue Feb 22, 2011 9:05 pm

Code: Select all
EditText text = (EditText) findViewById(R.id.some_edit_text);

Button butt = (Button) findViewById(R.id.your_button);
butt.setOnClickListener(new View.OnClickListener(){
     public void onClick() {
                 // Get edit text characters
                 String textInBox = text.getText():
                 // Remove last character
                 String newText = textInBox.substring(0, textInBox.length()-1);
                 // Update edit text
                 text .setText(newText);
     }
});



Pretty easy, more a java question that android
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: remove 1 letter for a edittext

Postby simondid » Wed Feb 23, 2011 11:02 pm

ty i realy have to get better at java!!!
simondid
Developer
Developer
 
Posts: 34
Joined: Mon Jan 31, 2011 3:45 pm

Re: remove 1 letter for a edittext

Postby blundell » Wed Feb 23, 2011 11:13 pm

Will fall over if you press the button when the edit text is empty ;-)
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: remove 1 letter for a edittext

Postby strider2023 » Mon Feb 28, 2011 11:12 am

Hey you can also add this line to the code...

String textInBox = text.getText();
if (textInBox.length() > 0)
String newText = textInBox.substring(0, textInBox.length()-1);
text .setText(newText);
strider2023
Senior Developer
Senior Developer
 
Posts: 126
Joined: Tue Mar 02, 2010 6:34 am
Location: Chennai, India

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 26 guests