如何从随机设置textview获取文本?

时间:2013-12-11 09:49:04

标签: android textview

我刚刚开始开发Android应用程序。这个应用程序只是一个随机引用的应用程序。 现在我试图从随机引用textview获取文本。但它返回false。

public class DisplayQuoteActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_quote);

    final String[] listQuotes = {"Quote one","A million or so","Really don't care","Looks like it","Day is done!"};

    Random randGen = new Random();
    int rando = randGen.nextInt(5);

    TextView textQuote = (TextView) findViewById(R.id.viewQuote);
    textQuote.setText(listQuotes[rando]);
}


/** Called when an user wants to share a quote**/
public void shareQuote(View view){
    Log.d("test_app","Share Quote been pushed");
    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); 
    sharingIntent.setType("text/plain");
    String shareBody = this.getString(R.id.viewQuote);// "Here is the share content body";
    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
    startActivity(Intent.createChooser(sharingIntent, "Share via"));
}

}

shareBody似乎是假的。

如何从textview viewQuote ??

获取文本

3 个答案:

答案 0 :(得分:1)

textQuote设为全局变量并替换

 String shareBody = this.getString(R.id.viewQuote);

 String shareBody = textQuote.getText().toString();

答案 1 :(得分:1)

尝试这样做:

String shareBody = ((TextView) findViewById(R.id.viewQuote)).getText();

答案 2 :(得分:1)

只使用这个

textQuote.getText().toString();

((TextView) findViewById(R.id.viewQuote)).getText().toString();