如何设置在片段一个TextView不同的字符串,同时点击不同的按钮? [英] How to set Different String in a Textview in Fragment while clicking different Button?

查看:245
本文介绍了如何设置在片段一个TextView不同的字符串,同时点击不同的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的Andr​​oid应用程序。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆