动态改变Fragment中的TextView(public void processMessage)。[英] Dynamically change TextView in Fragment (public void processMessage)

本文是小编为大家收集整理的关于动态改变Fragment中的TextView(public void processMessage)。的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在寻找一种创建聊天片段的方法.因此,我基本上试图使碎片动态.作为第一步,我试图更新我必须显示收到消息的文本视图.我正在使用XMPP作为客户端,并且通过以下功能接收消息:

        @Override
        public void processMessage(Chat arg0, org.jivesoftware.smack.packet.Message message) {

            // TODO Auto-generated method stub
            String from = message.getFrom();
            String body = message.getBody();
            System.out.println(String.format("Received message '%1$s' from %2$s", body, from));
            text.setText(String.format("'%1$s' from %2$s", body, from));
        }

关于在片段中动态更新文本视图的任何帮助.

谢谢

max

推荐答案

MyFragment的示例允许文本更新:

public class MyFragment extends Fragment {

    TextView textView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_b, container, false);
        textView = (TextView) view.findViewById(R.id.textView1);
        return view;
    }

    public void updateTextValue(CharSequence newText) {
        textView.setText(newText);
    }
}

Activity的示例Activity:MyFragment:

public class Activity extends Activity {

    MyFragment myFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_layout);

        FragmentManager manager = getFragmentManager();
        myFragment = (MyFragment) manager.findFragmentById(R.id.fragmentA);
    }

    @Override
    public void processMessage(Chat arg0, org.jivesoftware.smack.packet.Message message) {
        runOnUiThread(new Runnable() { 
            public void run() {             
                myFragment.updateTextValue("Hi fragment!");
            }
        }
    }
}

其他推荐答案

在ProcessMessage()函数中,您可以做getView().findViewById(id_of_text_view);

getView()返回onCreateView()

中创建的视图

http://developer.android.com/参考/android/app/fragment.html#getView%28%29

本文地址:https://www.itbaoku.cn/post/2091080.html

问题描述

I am looking for a way to create a chat fragment. So basically I am trying to make a fragment dynamic. As a first step I am trying to update a textview which I have to display messages received. I am using XMPP as a client and I am receiving message through the following function:

        @Override
        public void processMessage(Chat arg0, org.jivesoftware.smack.packet.Message message) {

            // TODO Auto-generated method stub
            String from = message.getFrom();
            String body = message.getBody();
            System.out.println(String.format("Received message '%1$s' from %2$s", body, from));
            text.setText(String.format("'%1$s' from %2$s", body, from));
        }

Any help regarding dynamically updating a textview in a fragment is appreciated.

Thanks

Max

推荐答案

Example of MyFragment that allow text update:

public class MyFragment extends Fragment {

    TextView textView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_b, container, false);
        textView = (TextView) view.findViewById(R.id.textView1);
        return view;
    }

    public void updateTextValue(CharSequence newText) {
        textView.setText(newText);
    }
}

Example of Activity that holds the MyFragment:

public class Activity extends Activity {

    MyFragment myFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_layout);

        FragmentManager manager = getFragmentManager();
        myFragment = (MyFragment) manager.findFragmentById(R.id.fragmentA);
    }

    @Override
    public void processMessage(Chat arg0, org.jivesoftware.smack.packet.Message message) {
        runOnUiThread(new Runnable() { 
            public void run() {             
                myFragment.updateTextValue("Hi fragment!");
            }
        }
    }
}

其他推荐答案

In the processMessage() function, you can do getView().findViewById(id_of_text_view);

getView() returns the View created in onCreateView()

http://developer.android.com/reference/android/app/Fragment.html#getView%28%29