简体   繁体   中英

Android Beginner: How can I build a dynamic Hello World app?

First things, first, I'm just starting with Android Application Development and would like to make a basic starter app. I have a Hello World app which will display the text Hello World when a button is pressed, but I would like to know how I can include a text box for a name, so the user can enter their name and then have the app say hello to them when they press the button.

Any help would be appriciated because I haven't a clue if I'm honest, I'm still figuring out what most of the code does but creating simple apps like this help me further my understanding.

Thank you,

Max

For the text box you'll probably want to use a EditText . Make sure to insert it into your layout xml and give it a name you can reference, like nameTextBox or something. It should look something like

<EditText android:layout_height="wrap_content" android:id="@+id/nameTextBox" android:layout_width="match_parent"></EditText>

Then when your activity runs, make sure to put it into a variable like you probably did your HelloButton . So

setContentView(R.layout.layout);
nameBox = (EditText) findViewById(R.id.nameTextBox);

Then when the user clicks the button go ahead and grab whatever is in the nameBox . If you're using a ClickListener for your button this would go inside the OnClick method, just before you print out the text.

String name = nameBox.getText().toString(); // Inherits toString from CharSequence
// code to display concatenate and display your hello string goes here 

I advise you to look at the samples provided by google on the android developer page.

http://developer.android.com/resources/browser.html?tag=sample

Look how things are done there and use that to make your own

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