开发者

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

开发者 https://www.devze.com 2023-03-18 02:08 出处:网络
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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号