【发布时间】:2019-06-07 13:50:47
【问题描述】:
我在同一个类中有两个 void 方法。 Method2 从 textview 获取数据并将其存储在字符串中。我想在 Method1 字符串中获取 Method2 字符串。 注意:这是一个安卓项目。 代码在这里:
class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
void Method2()
{
//this place i want to get str1 value like this:
// String str2=str1
}
void Method1(View view)
{
String str1=textView.getText().toString();
}
}
【问题讨论】:
-
使用全局变量怎么样?或者只是改变你的方法的返回值?
-
为什么不能将
str1声明为类级变量? -
要么在类级别声明变量str1,要么将Method1的返回类型更改为返回字符串并返回此str1并从method2调用method1。 void Method2() { //这个地方我想像这样获取 str1 值: // String str2=method1(view) } String Method1(View view) { return textView.getText().toString(); }