我正在尝试在MainActivity的标签上设置textview中的通知数量/数量。我通过使用接口成功地获取了我的片段和活动之间的数据。每当我收到通知时,它都会将号码传递给我的MainActivity。我可以显示数字,但我不知道如何在textview上实现和设置。
public void onDataPass(String data){ Log.d(“LOG”,“你好”+数据); }
我可以从上面的代码中获取价值。下面是我的logcat:
06-02 07:02:16.213 12310-12310 / D / LOG:你好通知数量:1
06-02 07:03:26.697 12310-12310 / D / LOG:hello通知数量:2
06-02 07:04:35.718 12310-12310 D / LOG:你好通知数量:3
但是,我正在尝试在textview上设置这个数字。即使我将“数据”声明为公开,我也会获得空值。我不确定如何将我的数据值放在textview中。有人可以帮我吗?
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
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);
}
}
答案 0 :(得分:1)
创建界面
public interface OnDataReceiverListener {
void onDataReceived(String myData);
}
在类中声明接口,其中您获取了需要在其他活动中传递的数据并为其创建setter ...假设MyReceiver.java
为类名
private static OnDataReceiverListener onDataReceiveListener;
public static void setOnDataReceiveListener(OnDataReceiverListener _listener) {
onDataReceiveListener = _listener;
}
在活动onCreate()
中调用此setter,假设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);
}
}
请勿忘记将setter设置为null
,因为界面是静态的。在onDestroy()
public void onDestroy() {
MyReceiver.setOnDataReceiveListener(null);
super.onDestroy();
}
概要,
将mTabLayout
声明为全局实例,在onDataReceived
回调中设置徽章文字。