【问题标题】:Check tab from activity to fragment in android在android中检查从活动到片段的选项卡
【发布时间】:2020-02-20 06:12:35
【问题描述】:

enter image description here

看起来像这样,我在这里使用未回答选项卡的适配器类,实际上我需要当我们选择单选按钮并单击下一步然后选项卡将被选为已回答 这是未选择的代码 1.从适配器类

 public View getSelectedTabView(int position) {
    View view = LayoutInflater.from(context).inflate(R.layout.custom_tab, null);
    ImageView imageView = view.findViewById(R.id.custom_tab_circle);
    TextView textView = view.findViewById(R.id.custom_tab_textView);
    //textView.setText(mfragmentTitlelist.get(position+1));
    //textView.setText(position);
    //textView.setText((CharSequence) fragmentList.get(position));
    StringBuilder builder = new StringBuilder();

    builder.append(position+1).toString();
    textView.setText(builder.toString());
    imageView.setImageResource(R.drawable.selected_circle);
    // here is image will be transparent
    imageView.setAlpha(0.5f);


    //.setTextColor(ContextCompat.getColor(context, R.color.yellow));
    //ImageView tabImageView = view.findViewById(R.id.tabImageView);
    //imageView.setImageResource(mFragmentIconList.get(position));
    //imageView.setColorFilter(ContextCompat.getColor(context, R.color.background_color), PorterDuff.Mode.SRC_ATOP);
    return view;
}

**2。现在我们在 Activity.java 中调用这段代码 **

  highLightCurrentTab(position);


  private void highLightCurrentTab(int position) {
    TabLayout.Tab tab = tabLayout.getTabAt(position);
    assert tab != null;
    tab.setCustomView(null);
    tab.setCustomView(adapter.getSelectedTabView(position));
    /*tab.setCustomView(adapter.getPageTitle(position).charAt(position));*/

}

所以请帮助我在选择答案时该怎么做,我的标签图像将被选为已回答... 谢谢..

【问题讨论】:

    标签: android android-fragments android-activity


    【解决方案1】:

    你可以用它来监听你的tablayout;

      tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
        }
    
        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
        }
    
        @Override
        public void onTabReselected(TabLayout.Tab tab) {
    
        }
    });
    

    您还可以使用此代码检查是否选择了任何单选按钮;

    if (radioGroup.getCheckedRadioButtonId() == -1)
    {
      // no radio buttons are checked
    }
    
    else
    {
      // one of the radio buttons is checked
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-31
      相关资源
      最近更新 更多