How to set text of TextView ?

2019-03-27 20:33发布

问题:

I am facing a problem of setting the text to TextView in android my code is :

public class Main extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button = (Button) findViewById(R.id.button1);
        final TextView text = (TextView) findViewById(R.id.textView1);
        final EditText input = (EditText) findViewById(R.id.editText1);
        final String string = input.getText();
        button.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                text.setText(string);                   
            }
        });
    }
}

if I write

    final Editable string = input.getText();

then it works.....!!!!

Now I want to send data of EditText to next Activity like this :

public class Main extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button = (Button) findViewById(R.id.button1);
        final TextView text = (TextView) findViewById(R.id.textView1);
        final EditText input = (EditText) findViewById(R.id.editText1);
        final Editable string = input.getText();
        button.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent intent = new Intent(Main.this, Second.class);
                intent.putExtra("thetext", string);
                startActivity(intent);
            }
        });
    }
}

and in Second.java class I get StringExtra in this way:

public class Second extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);

        TextView text = (TextView) findViewById(R.id.textView2);
        String string = getIntent().getExtras().getString("thetext", "not found");
        text.setText(string); /// Here the text is not shown but the default message "not found" is set to `TextView`
    }
}

Please give me way to proceed in development.

回答1:

I think the problem is you're actually putting an "Editable" in the intent, not a String. Although close, they're not the same thing. If you toString() your Editable to get a String object and put that in the intent, you should be able to get it back out with getString like you're doing.



回答2:

The problem should be, you're sending Editable, not String. Try this:

final String string = input.getText().toString();


回答3:

     private TextView mTextView;
     private String mString;

mTextView = (TextView) findViewById(R.id.tv);
mString = "hello everyone ! how r u?";
mTextView.setText(mString);


回答4:

Try something like this:

public class Main extends Activity {
    EditText input;

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

        TextView text = (TextView) findViewById(R.id.textView1);
        input = (EditText) findViewById(R.id.editText1);

        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(Main.this, Second.class);
                intent.putExtra("thetext", input.getText().toString());
                startActivity(intent);
            }
        });
    }
}

(Hint: The easiest way to post code is to paste your code, select it, and use crtl+k to indent/format it.)



回答5:

According to the android docs, the name of the string put into extras must include a package prefix... i.e. som.arshay.dev.thetext Secondly, getExtras() returns a bundle, which is not what you added. You need getStringExtra( name )



回答6:

In this line use

final String string = input.getText().toString(); 

instead of

final String string = input.getText();