单击一个活动中的TextView以显示另一个活动中的文本

时间:2014-02-11 14:17:23

标签: android

我正在制作一个引号应用,我创建了一个名为Einstein.java的活动,其中我有大约50个带引号的文本视图。我想使引号可单击,以便它们一次弹出另一个活动(QuotesCLick.java)单引号。这是我的代码:

Einstein.java:

public class Einstein extends Activity {
private RandomQuotes mRandomQuotes = new RandomQuotes();



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

public String getQuotes(){

    String quote = "";
    return quote;
 }
 }

QuotesClick.java:

public class QuoteClick extends Activity implements View.OnClickListener{
private Einstein mEinstein = new Einstein();

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

final TextView quotesView = (TextView) findViewById(R.id.textView);
TextView getQuote = (TextView) findViewById(R.id.textView);

@Override
public void onClick(View view) {
    String quote = mEinstein.getQuotes();

    quotesView.setText(quote);
    }
    }

1 个答案:

答案 0 :(得分:1)

当您点击项目时,只需定义Intent,将所需数据放入其中并开始第二项活动,您将可以获取传递的数据。

@Override
public void onClick(View view) {
  final Intent myIntent = new Intent(this, YourSecondActivity.class);
  myIntent.putExtra("myquote", "This is the quote I want to pass!");
  startActivity(myIntent);
}

现在,在您的第二个Activity中,只需恢复该数据:

final Intent passedIntent = getIntent();
final String passedQuote = passedIntent.getStringExtra("myquote");
final TextView quotesView = (TextView) findViewById(R.id.textView);
quotesView.setText(passedQuote);