通过界面更新/设置文本 [英] Update/set text through interface

查看:110
本文介绍了通过界面更新/设置文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在MainActivity标签上的textview中设置通知的数量/数量.我已经通过使用接口成功获取了片段和活动之间的数据.每当我收到通知时,它将把该号码传递给我的MainActivity.我可以显示该数字,但不知道如何在textview上实现和设置.

I'm trying to set the number/count of notifications in a textview on a tab on my MainActivity. I have successfully get the data between my fragment and activity through the use of interface. Every time when I received a notification, it will pass the number to my MainActivity. I am able to display the number but I do not know how to implement and set on my textview.

public void onDataPass(字符串数据){ Log.d("LOG","hello" + data); }

public void onDataPass(String data) { Log.d("LOG","hello " + data); }

我能够从上面的代码中获取价值.以下是我的logcat:

I am able to get value from the above code. Below is my logcat:

06-02 07:02:16.213 12310-12310/D/LOG:您好通知数量:1

06-02 07:02:16.213 12310-12310/ D/LOG: hello Number of notifications: 1

06-02 07:03:26.697 12310-12310/D/LOG:您好通知数量:2

06-02 07:03:26.697 12310-12310/ D/LOG: hello Number of notifications: 2

06-02 07:04:35.718 12310-12310 D/LOG:您好通知数量:3

06-02 07:04:35.718 12310-12310 D/LOG: hello Number of notifications: 3

但是,我正在尝试在textview上设置该数字.即使将我的数据"声明为公开,我也得到了空值.我不确定如何将数据的值放在textview中.有人可以帮我吗?

However, I'm trying to set this number on my textview. I'm getting null value even after declaring my "data" as public. I'm not sure how do I put the value of my data in the textview. Can anybody help me with this?

   if(tab != null && tab.getCustomView() != null) {
        TextView b = (TextView) tab.getCustomView().findViewById(R.id.badge);
        if (b != null) {
            b.setText(data);
            Log.d("hello1", "test1" + data);
        }

以下是我完整的MainActivity代码: MainActivity.java

Below is my full MainActivity code: MainActivity.java

public class MainActivity extends AppCompatActivity implements NotificationFragment.OnDataPass {
    public String data;
    private String name;
    private String email;
    private TextView badgeText;

    public void onDataPass(String data) {
        Log.d("LOG","hello " + data);
    }

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


        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        ActionBar ab = getSupportActionBar();
        ab.setTitle("Test");

        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        if (viewPager != null) {
            setupViewPager(viewPager);
        }

        final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);



        final int[] tabIcon = new int[]{
                R.drawable.tab_item_remit_state,
                R.drawable.tab_item_account_state,
                R.drawable.tab_item_notification,
        };

        tabLayout.getTabAt(0).setIcon(tabIcon[0]);
        tabLayout.getTabAt(1).setIcon(tabIcon[1]);
        tabLayout.getTabAt(2).setIcon(tabIcon[2]);

        TabLayout.Tab tab = tabLayout.getTabAt(2);
        tab.setCustomView(R.layout.badged_tab);


        if(tab != null && tab.getCustomView() != null) {
            TextView b = (TextView) tab.getCustomView().findViewById(R.id.badge);
            if (b != null) {
                b.setText(data);
                Log.d("hello1", "test1" + data);
            }
            final View v = tab.getCustomView().findViewById(R.id.badgeCotainer);
            View a = tab.getCustomView();

            a.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View a, MotionEvent event) {
                    v.setVisibility(View.INVISIBLE);
                    return false;
                }
        });

        if (v != null ) {
            v.setVisibility(View.VISIBLE);

        }
    }

推荐答案

创建界面

public interface OnDataReceiverListener {
   void onDataReceived(String myData);
}

在类中声明接口,您在其中获取了需要在其他活动中传递的数据并为此创建了setter ...假设类名为MyReceiver.java

Declare Interface in class, where you got data that needs to be passed in other activity and create setter for that... Assuming MyReceiver.java for class name

private static OnDataReceiverListener onDataReceiveListener;
public static void setOnDataReceiveListener(OnDataReceiverListener _listener) {
    onDataReceiveListener = _listener;
}

在活动的onCreate() 中调用此设置器,假设MainActivity

MyReceiver.setOnDataReceiveListener(this);

// This will overided
@Override
public void onDataReceived(String myData) {
    // Process your data 
    TextView b = (TextView) mTabLayout .getTabAt(2).getCustomView().findViewById(R.id.badge);
    if (b != null) {
        b.setText(myData);
    }       
}

不要忘记将设置器设置为null,因为界面是静态的.在onDestroy()

Don't forget to set setter to null as interface is static. Do this job in onDestroy()

public void onDestroy() {
   MyReceiver.setOnDataReceiveListener(null);
   super.onDestroy();
}

摘要, 将mTabLayout声明为全局实例,在onDataReceived回调中设置徽章文本.

Summary, Declare mTabLayout as global instance, set badge text in onDataReceived callback.

这篇关于通过界面更新/设置文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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