![](/img/trans.png)
[英]How to make that all works together.Intent, onClickListener, Several classes, Show another activity
[英]How to make onClickListener receive Intent information from another activity
我试图使按钮使用来自另一个活动的一些数据,我确实进行了搜索,但是我不知道如何操作。
onClickListener不起作用。 这是我的onCreate内的代码:
super.onCreate(savedInstanceState);
setContentView(R.layout.show_message);
Intent intent = getIntent();
String msg = intent.getStringExtra("MESSAGE");
ScrollTextView scrolltext = (ScrollTextView) findViewById(R.id.scroll_text);
scrolltext.setText(msg);
scrolltext.setTextSize(220);
scrolltext.startScroll();
super.onResume();
DialogFragment mDialog = new GeneralDialogFragment();
mDialog.show(getSupportFragmentManager(), "Dialog TAG");
ImageButton mImageButton = (ImageButton)findViewById(R.id.image_button);
mImageButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
//I'm sure this is not the to make this button works. What code should I put here to pass this data to the ScrollTextView?
scrolltext.setText(msg);
scrolltext.setTextSize(220);
scrolltext.startScroll();
}
});
您编写的这两行仅用于从先前正在执行的活动中获取信息
Intent intent = getIntent();
String msg = intent.getStringExtra("MESSAGE");
如果您想随时使用其他活动中的数据,则必须使用以下几行将数据保存在上一个活动中:
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("msg", "MESSAGE");
editor.commit();
并在您的活动中这样调用它:
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String yourmessage = prefs.getString("msg", null);
if (restoredText != null) {
String name = prefs.getString("msg", "No name defined");//"No name defined" is the default value.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.