<android> 常用但容易忘记的一些代码和技巧 汇总(个人笔记)

1.edittext只允许输入小数点后x位 edittext设置区域   android:minLines="7"  android:gravity="top"能保证在最上端



editText.addTextChangedListener(textWatcher);


    TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {
            if (s.toString().contains(".")) {
                if (s.length() - 1 - s.toString().indexOf(".") > 1) {
                    s = s.toString().subSequence(0,
                            s.toString().indexOf(".") + 2);
                    in_info13.setText(s);
                    in_info13.setSelection(s.length());
                }
            }
            if (s.toString().trim().substring(0).equals(".")) {
                s = "0" + s;
                in_info13.setText(s);
                in_info13.setSelection(2);
            }
            if (s.toString().startsWith("0")
                    && s.toString().trim().length() > 1) {
                if (!s.toString().substring(1, 2).equals(".")) {
                    in_info13.setText(s.subSequence(0, 1));
                    in_info13.setSelection(1);
                    return;
                }
            }
        }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
        }
        @Override
        public void afterTextChanged(Editable s) {
        }
    };








        
2.版本更新时的rom适配问题 ===》 拉起安装界面  N以上


        Intent intent = new Intent(Intent.ACTION_VIEW);
//判断是否是AndroidN以及更高的版本
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(mContext, "com.xxxxxxx.xxx.fileprovider", file);
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }


        mContext.startActivity(intent);




3.webview监听地址变化 应在loadUrl后    webView.setWebViewClient(new MyWebViewClient());  在之前绝对不走 


4.代码截屏:
  private void screenshot()
    {
        // 获取屏幕
        View dView = getWindow().getDecorView();
        dView.setDrawingCacheEnabled(true);
        dView.buildDrawingCache();
        Bitmap bmp = dView.getDrawingCache();
        if (bmp != null)
        {
            try {
                // 获取内置SD卡路径
                String sdCardPath = Environment.getExternalStorageDirectory().getPath();
                // 图片文件路径
                String filePath = sdCardPath + File.separator + "screenshot.png";


                File file = new File(filePath);
                FileOutputStream os = new FileOutputStream(file);
                bmp.compress(Bitmap.CompressFormat.PNG, 100, os);
                os.flush();
                os.close();
                Toast.makeText(this, "屏幕截取成功", Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
            }
        }
    }




5.系统分享:


    public static void shareMsg(Context context, String activityTitle,
                                String msgTitle, String msgText, String imgPath) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        if (imgPath == null || imgPath.equals("")) {
            intent.setType("text/plain"); // 纯文本
        } else {
            File f = new File(imgPath);
            if (f != null && f.exists() && f.isFile()) {
                intent.setType("image/jpg");
                Uri u = Uri.fromFile(f);
                intent.putExtra(Intent.EXTRA_STREAM, u);
            }
        }
        intent.putExtra(Intent.EXTRA_SUBJECT, msgTitle);
        intent.putExtra(Intent.EXTRA_TEXT, msgText);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(Intent.createChooser(intent, activityTitle));
    }






6:


/**
     * 网络地址获取bitmap
     * @param url
     * @return
     */
    public Bitmap returnBitMap(String url){
        URL myFileUrl = null;
        Bitmap bitmap = null;
        try {
            myFileUrl = new URL(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        try {
            HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
            conn.setDoInput(true);
            conn.connect();
            InputStream is = conn.getInputStream();
            bitmap = BitmapFactory.decodeStream(is);
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }






10.listview嵌套viewpager viewpager切记不要继承FragmentPagerAdapter  应该继承PagerAdapter  否则会出现刷新白屏现象




11.listview嵌套viewpager 报错:java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.


解决方案:
        //灭屏或上下滚动列表时报错:The specified child already has a parent. You must call removeView() on the child's parent first.
        //下面代码解决  instantiateItem方法addView之前加上:
        ViewParent vg = fraList.get(position).getParent(); 
        if (vg != null) {
            ViewGroup group = (ViewGroup) vg;
            group.removeView(fraList.get(position));
        }






 该帖会及时更新!

猜你喜欢

转载自blog.csdn.net/csdn_lg_one/article/details/78999931