Java 在android中,如何向菜单项方法发送字符串?
我正在开发一个android应用程序,用户按下一个按钮,生成一个文本,然后有一个菜单选项来共享/导出文本 以下是相关的代码部分(为了清楚起见,我不粘贴整个代码,但是如果您需要任何东西,请告诉我) 我的问题: 如何将SomeMethod方法生成的字符串(即savedText,请参见代码第5行中的注释)发送到exportPage方法?目前,代码从初始化的字符串值中获取savedText字符串的值,而不是由SomeMethod方法生成的值 我尝试过的: 在寻找类似问题的答案时,我发现这里描述的方法是: 我试图相应地修改代码,如下所示:Java 在android中,如何向菜单项方法发送字符串?,java,android,string,methods,Java,Android,String,Methods,我正在开发一个android应用程序,用户按下一个按钮,生成一个文本,然后有一个菜单选项来共享/导出文本 以下是相关的代码部分(为了清楚起见,我不粘贴整个代码,但是如果您需要任何东西,请告诉我) 我的问题: 如何将SomeMethod方法生成的字符串(即savedText,请参见代码第5行中的注释)发送到exportPage方法?目前,代码从初始化的字符串值中获取savedText字符串的值,而不是由SomeMethod方法生成的值 我尝试过的: 在寻找类似问题的答案时,我发现这里描述的方法是:
TextView resp = (TextView) findViewById(R.id.TextOutput);
resp.setText(savedText);
exportPage(); // <<< attempting to send the string to the exportPage method
public void exportPage() {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, this.savedText); //<<< modified
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
}
我得到一个错误,因为exportPage方法需要一个字符串。我试图用exportPage(savedText)来修改它当然,现在它不识别字符串savedText,并希望我初始化它(这样做,我回到原来的问题,获取全局字符串值,而不是生成的字符串值)
正如我所说,我是一个初学者,所以我敢肯定这对我来说是一个非常简单的错误,但我就是想不出来。有什么想法吗?将resp TextView声明为全局变量,然后直接从TextView获取文本值:
private TextView resp = null;
FloatingActionButton button = (FloatingActionButton) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String savedText = SomeMethod(); //the string used here is obtained through another method
resp = (TextView) findViewById(R.id.TextOutput);
resp.setText(savedText);
}
});
public void exportPage() {
String savedText = this.resp != null ? this.resp.getText().toString() : "";
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, savedText);
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
}
用这个
public void exportPage() { //<<<< modified
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, resp.getText().tostring());
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
}
public void exportPage(){/您好如果有帮助,请尝试此方法
1) 从onClick方法本身调用该方法
2) 将参数添加到获取值的方法中
FloatingActionButton button = (FloatingActionButton) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String savedText = SomeMethod(); //the string used here is obtained through another method
TextView resp = (TextView) findViewById(R.id.TextOutput);
resp.setText(savedText);
//Call method exportPage so that it can get value on the go
exportPage(String savedText);
}
});
public void exportPage(String savedText) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, savedText);
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
}
什么是将resp TextView声明为全局变量的正确方法(即在何处和如何声明?)我认为将它移动到TextView resp=(TextView)findViewById(R.id.TextOutput);
onClick方法之外(我将它放在SetContentView(R.layout.activity.main);
行可以工作,但我仍然得到一个“找不到符号方法getText”错误。@DigitalDracula我已经编辑了我的答案,请检查!成功!我确认这是一个优雅、简单的解决方案。
private TextView resp = null;
FloatingActionButton button = (FloatingActionButton) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String savedText = SomeMethod(); //the string used here is obtained through another method
resp = (TextView) findViewById(R.id.TextOutput);
resp.setText(savedText);
}
});
public void exportPage() {
String savedText = this.resp != null ? this.resp.getText().toString() : "";
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, savedText);
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
}
public void exportPage() { //<<<< modified
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, resp.getText().tostring());
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
}
FloatingActionButton button = (FloatingActionButton) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String savedText = SomeMethod(); //the string used here is obtained through another method
TextView resp = (TextView) findViewById(R.id.TextOutput);
resp.setText(savedText);
//Call method exportPage so that it can get value on the go
exportPage(String savedText);
}
});
public void exportPage(String savedText) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, savedText);
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
}