如何设置在片段一个TextView不同的字符串,同时点击不同的按钮? [英] How to set Different String in a Textview in Fragment while clicking different Button?
问题描述
我是新的Android应用程序。
I'm new to Android app programming.
我有两个片段 - fragmentA.xml
和 fragmentB.xml
I have two Fragments - fragmentA.xml
and fragmentB.xml
.
在碎裂
有两个按钮 - 按钮一次(ID: btnOne
)和两个按钮(ID: btnTwo
)。
In fragmentA
there are two Buttons - Button One (id: btnOne
) and Button Two (id: btnTwo
).
在 fragmentB
有TextView的(ID- 的TextView
)
In fragmentB
there is TextView (id- textView
)
在 fragmentB
Java类有两个字符串
- 字符串1
和字符串2
In fragmentB
java class there are two String
- string1
and string2
两个按钮指 fragmentB
。
我要两个设置的TextView
文本字符串1
当我preSS按钮一次
和字符串2
当我preSS两个按钮。
I want two set the textView
text to string1
when I press Button One
and string2
when I press Button Two.
我怎么能这样做?
我用的是按照code fragmentB
Java类,只显示字符串1
I was using following code in fragmentB
java class to display only string1
public View onCreateView(
LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.oop_page,container,false);
TextView textV = (TextView)rootView.findViewById(R.id.textView);
textV.setText(string1);
return rootView;
}
我可以使用任何的if-else语句在这里?
Can I use any if-else statement here?
感谢名单
推荐答案
在项目中创建一个全球
类,并定义一个公开字符串。
Create a Global
class in your project and define a public string ..
public class Global{
public static String TEXT = "";
}
在你的片段A
更改此 TEXT
按钮单击事件。
In your Fragment A
change this TEXT
on button click event..
but1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Global.TEXT = "string 1";
}
});
but2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Global.TEXT = "string 2";
}
});
一样的,如果你有其他的按钮
same as on other button if you have
在你的 FragmentB
只是的setText()
....这将需要化酶值更改为文本
In your FragmentB
just setText()
....it will take lase value change to Text
text.setText(Global.TEXT);
这篇关于如何设置在片段一个TextView不同的字符串,同时点击不同的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!