简体   繁体   中英

Viewing Dynamic text in Android

I am trying to print Hello + written text in text box (As I posted earlier). Here's the code I have done. But instead of printing Hello+written text, its only printing Hello after clicking the button.

Any suggestion will be appreciated. Thanks.

public class myActivity1 extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

      /* // testing add button
         setContentView(R.layout.content_layout_id);
      */

        final TextView nameText= (TextView) findViewById(R.id.entry);  
        //final TextView tv1 = new TextView(this);
        final TextView tv2 = new TextView(this);

        tv2.setText("Hello"+ nameText.getText().toString());

        final Button button = (Button) findViewById(R.id.Button01);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                setContentView(tv2);


            }
        });

    }
}

It is not printing anything after "Hello", because nameText is empty.

Now, perhaps nameText is really an EditText , and you are trying to fill it in at runtime. If so, you need to check the contents of nameText in the OnClickListener . Right now, you are checking it in onCreate() , before the UI is even presented to the user.

Should your setContentView(tv2) line be:

tv2.setText("Hello"+ nameText.getText().toString());

Instead?

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