美文网首页
Android开发中记录常见问题&解决方案

Android开发中记录常见问题&解决方案

作者: 艾曼大山 | 来源:发表于2019-04-11 15:01 被阅读0次

为什么会有这篇文章?

俗话说的号,好记性不如烂笔头!程序员的脑子也是会生锈滴,有些东西平时不常用容易忘记。这是平时工作中常见的功能和遇到的一些问题,记录下来,遇到问题时可以先来这里看下有没有你想要的。

1.AlertDialog在安卓8.0系统中使用不显示"取消 确定"按钮。
2.给TabLayout添加消息红点。
3.登录注册页面软键盘覆盖按钮。
4.切换密码EditText加密和不加密
图片.png
1.AlertDialog在安卓8.0系统中使用不显示按钮 解决方案

通过AlertDialog.BUTTON_POSITIVE、AlertDialog.BUTTON_NEGATIVE,动态获取“取消”和“确定”按钮view,设置view的颜色就可以了。在获取view和设置view颜色的代码必须要在dialog.show()后面调用才会生效。看下方代码

按钮没显示.png
 AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("提示");
    builder.setMessage("要删除这张照片吗?");
    builder.setNegativeButton("取消", null);
    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
        //移除当前图片刷新界面
      }
    });
    AlertDialog dialog = builder.create();
    dialog.show();

 //**在获取view和设置view颜色的代码必须要在dialog.show()后面调用才会生效。
    dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.BLUE);
    dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.BLACK);
2.给TabLayout添加消息红点。

平时用TabLayout+ViewPage来实现功能,有时候需要给TabLayout的某个标题添加红点标识。

  • 首先和正常代码一样给viewPage设置adapter、tabLayout设置addOnTabSelectedListener
  • 循环tabLayout的子tab,根据实际需求改变某个子tab添加红点
  • 设置tabLayout的Title选中和为选中的状态,当点击有红点的title后应把红点消失
给tab添加红点.png
   //viewPage设置好adapter后
   viewPager.setAdapter(pagerAdapter);
    //循环mTabLayout的子view
    for (int i = 0; i < mTabLayout.getTabCount(); i++) {
      /**在这里判断每个TabLayout的内容是否有更新,来设置小红点是否显示,假设第二个子view需要有红点**/
      if(i == 1) {
          //获取TabLayout第i个子View
          Tab tab = mTabLayout.getTabAt(i);
          //给tab设置View,R.layout.tab_msg是自定义的View
          tab.setCustomView(R.layout.tab_msg);
          //找到控件 填充数据
          TextView tab_title = tab.getCustomView().findViewById(R.id.tab_title);
          tab_title.setText("待审核");
          TextView tab_red = tab.getCustomView().findViewById(R.id.tab_red);
          tab_red.setText("2");
      }
    }
    //给TabLayout设置监听TabSelectedListener 
    mTabLayout.addOnTabSelectedListener(new TabSelectedListener(mContext,viewPager));
public class TabSelectedListener implements OnTabSelectedListener {

  private ViewPager viewPager;
  private Context context;

  public TabSelectedListener(Context context,ViewPager viewPager) {
    this.context = context;
    this.viewPager = viewPager;
  }

  @Override
  public void onTabSelected(Tab tab) {
    viewPager.setCurrentItem(tab.getPosition());
      /**
     * 设置选中时的状态
     * 先判断当前tab是否添加了CustomView
     * 如果该tab添加了CustomView,隐藏红点 设置字体颜色为高亮
     */
    if (tab.getCustomView() != null) {
      View customView = tab.getCustomView();
      customView.findViewById(R.id.tab_red).setVisibility(View.GONE);
      TextView textView = customView.findViewById(R.id.tab_title);
      textView.setTextColor(ContextCompat.getColor(context,R.color.blue));
    }

  }

  @Override
  public void onTabUnselected(Tab tab) {
     /**
     * 设置未选中时的状态
     * 先判断当前tab是否添加了CustomView
     * 如果该tab添加了CustomView,设置字体不高亮
     */
    if (tab.getCustomView() != null) {
      View customView = tab.getCustomView();
      TextView textView = customView.findViewById(R.id.tab_title);
      textView.setTextColor(ContextCompat.getColor(context,R.color.gray_78));
    }

  }

  @Override
  public void onTabReselected(Tab tab) {

  }
}
3.登录注册页面软键盘覆盖按钮。

平时开发登录一些页面时,如果我们的手机屏幕不是很大的话,软键盘会覆盖登录按钮,影响视觉影响操作。首先我们先获取最外层布局的高度,然后再获取View可见区域的bottom,通过这两个值相减就获取的软键盘的高度,再获取登录按钮Y轴值可算出按钮一下空白区域的高度,最后通过软键盘的高度和屏幕高度的1/4相比&&软键盘高度大于底部空白区域高度来判断是否会覆盖登录按钮。代码中会详细的注释!

软键盘顶起按钮.gif
//在initView中先获取底部按钮底部Y值
loginBtn.post(new Runnable() {
     @Override
     public void run() {
       btnBottom = loginBtn.getBottom();
     }
   });

 /**
   * @param mainView 最外层View
   * @param bottomView 底部登录按钮
   */
  public void addLayoutListener(final View mainView, final View bottomView) {
  /**
     * OnGlobalLayoutListener 是ViewTreeObserver的内部类,
     * 当一个视图树的布局发生改变时,可以被ViewTreeObserver监听到
     */
    mainView.getViewTreeObserver()
        .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
          @Override
          public void onGlobalLayout() {
            Rect rect = new Rect();
            // 获取mainView在窗体的可视区域
            mainView.getWindowVisibleDisplayFrame(rect);
            //获取屏幕的高度
            int screenHeight = mainView.getRootView().getHeight();
            //获取不可视高度(软键盘的高度)
            int mainInvisibleHeight = screenHeight - rect.bottom;
            //获取底部空白区域的高度
            int bottonHeight = screenHeight - btnBottom;

           /**
             * 判断不可视区域高度是否大于屏幕高度的四分之一 并且
             * 不可是区域高度大于底部空白区域高度 才会覆盖按钮
             * 
             * 否则scrollTo到顶部
             */
            if (mainInvisibleHeight > screenHeight / 4 && mainInvisibleHeight > bottonHeight) {
              int[] location = new int[2];
              bottomView.getLocationInWindow(location);
              int srollHeight = (location[1] + bottomView.getHeight()) - rect.bottom;
              mainView.scrollTo(0, srollHeight);
            } else {
              mainView.scrollTo(0, 0);
            }

          }
        });
  }

4.切换密码EditText加密和不加密

平时做登录时会遇到密码EditText切换加密和不加密,本以为动态改变inputType就行了,可就不如你所愿。

切换密码加密.gif
  if (isPwd) {
          //设置可见密码
          etUserPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
        } else {
          //设置加密
          etUserPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }
        //重新加载数据
        etUserPwd.setSelection(etUserPwd.getText().toString().length());

相关文章

网友评论

      本文标题:Android开发中记录常见问题&解决方案

      本文链接:https://www.haomeiwen.com/subject/mgwziqtx.html