• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Get text value of textbox?

Hi I'm trying to run a system command that requires user input which I need to take and use in the command. I'm also a developer for iPhone so in the iOS SDK it would theoretically be like this, if Apple hadn't sandboxed everything.

-(IBAction)command {
system("echo %s", textfield.text");
}

Or something like that, I don't know, I haven't done it in a while. But can anyone teach me how to setup a textbox and also convert that code above to Java? Thanks, it would be very much appreciated. :)
 
Hi I'm trying to run a system command that requires user input which I need to take and use in the command. I'm also a developer for iPhone so in the iOS SDK it would theoretically be like this, if Apple hadn't sandboxed everything.

-(IBAction)command {
system("echo %s", textfield.text");
}

Or something like that, I don't know, I haven't done it in a while. But can anyone teach me how to setup a textbox and also convert that code above to Java? Thanks, it would be very much appreciated. :)

You asked a pretty open ended question so I will do my best to answer it. To setup a textbox where you can edit information I would look into the EditText view (EditText | Android Developers). This can either be added to a layout .xml or dynamically added in your activity.

If you want to access the text in the edittext it would go something like this

EditText et = (EditText)findViewById(R.id.idofedittext);
String text = et.getText();

You can then display the text to the user through either just editing a textedit or popping up an alertdialog.

Hopefully I helped you out.
 
Yo, OK. Here's what you need to do:-

Put an EditText in your UI, and call getText().toString() on it when you need the value that the user typed in.

or, by using GetText(): Button mButton;
EditText mEdit;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mButton = (Button)findViewById(R.id.button);
mEdit = (EditText)findViewById(R.id.edittext);

mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
Log.v("EditText", mEdit.getText().toString());
}
});
}

I hope this helps.
 
Thanks! I think I got the getText method working. You guys are so much nicer than the iPhone devs! But I have one more question. Can someone post an example code for running a system command such as echo, with the getText method? Thanks so much! :)
 
Java and Android provide a couple different ways of displaying text. The most common one in Java is System.out.println("text"); although the Android SDK provides a usefull Log class where you can write text to LogCat output. The API info can be found here: Log | Android Developers
 
Back
Top Bottom