简体   繁体   中英

How to display inputted text in a TextView after clicking a button in Android studio?

So I am experimenting with Android Studio. I am stuck in the part where I want my "Submit" button to display the user inputted text underneath the page.

Button submit is the button. "name" and "email" are the EditText variables aka text fields My TextView variable is outputText

So after the user enters random stuff in both text fields and click submit, I want my outputText to display both texts that were inputted in 2 separate lines

ex:

John Smith

john.smith@gmail.com

The code below is my .java code for the activity. I need to know how to initialize the new intent in a way that lets me do the 4 lines below it. Thanks.

public void buttonOnClick(View v){
    Button submit = (Button) v;

    submit.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent i =
                    new Intent(...?...);
            startActivity(i);
        }
    });
    editName = (EditText) findViewById(R.id.name);
    editEmail = (EditText) findViewById(R.id.email);

    textout = (TextView) findViewById(R.id.textView);
    textout.setText(editName.getText()+" "+editEmail.getText());


}    

I want my outputText to display both texts that were inputted in 2 separate lines

Use <br> and Html.fromHtml between text to add new line:

String strText=(String)editName.getText()+"<br>"+(String)editEmail.getText();
textout.setText(Html.fromHtml(strText));

how to initialize the new intent in a way that lets me do the 4 lines below it...

Intent i =new Intent(CurrentActivityName.this,TargetActivityName.class);
startActivity(i);

For print in next line

textout.setText(editName.getText()+"\n"+editEmail.getText());

For intent

Intent i =new Intent(FirstActivity.this,SecondActivity.class);
startActivity(i);

Ok I was able to accomplish what I wanted without an Intent initialized. Here's the code:

    public void buttonOnClick(){
    Button submit = (Button) findViewById(R.id.submit);
    editName = (EditText) findViewById(R.id.name);
    editEmail = (EditText) findViewById(R.id.email);
    textout = (TextView) findViewById(R.id.outputText);

    submit.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
             textout.setText(editName.getText()+"\n"+editEmail.getText());
        }
    });


}    

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