[toc]
Tags: android
人口智能管理系统文档技术整理
标签(空格分隔):
一、 Irecycleview
Irecycleview是自定义的recyclerview,支持上拉刷新、下拉加载更多结合
How to use 使用1
Step 1. 引用Irecycleview包
- include ':app', ':library:irecyclerview' //settings.gradle
- compile project(':library:irecyclerview')//build.gradle(Module:app)
Step 2.在布局文件中引用IRecyclerView
<com.aspsine.irecyclerview.IRecyclerView
android:id="@+id/population_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
app:loadMoreEnabled="true"
app:refreshEnabled="true"
app:refreshHeaderLayout="@layout/layout_irecyclerview_refresh_header" />:
Step 3. 新建Viewholder Item项的布局文件(呈现数据的样式)
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="@color/gray6"
app:cardCornerRadius="6dp"
app:cardElevation="2dp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/family_cb_control"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:visibility="gone">
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<ImageView
android:id="@+id/familyIco"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/ic_familyheader" />
</LinearLayout>
<LinearLayout
android:id="@+id/family_detail"
style="@style/listbox_linearLayout">
<TextView
android:id="@+id/familyName"
style="@style/listbox_linearLayout_name"
android:text="阿不来提 奥斯曼" />
<TextView
android:id="@+id/familyIDcard"
style="@style/listbox_linearLayout_id"
android:text="650212099012032210" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/locateIcon"
>
<ImageButton
android:id="@+id/locateBtn"
style="@style/listbox_linearLayout_icon_location" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="1dp"
>
<ImageButton android:id="@+id/detailBtn"
style="@style/listbox_linearLayout_icon_detail" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
Step 4. 在使用Irecycleview 的Activity中使用:
- 初始化:
mRecyclerView=(IRecyclerView)findViewById(R.id.population_recycler_view);
LinearLayoutManager pLayoutManager= new LinearLayoutManager(this);
mLayoutManager=pLayoutManager;
// 设置布局管理器
mRecyclerView.setLayoutManager(mLayoutManager);
// 设置adapter
mAdapter = new search_population_adapter(this, populationPresenterImpl);
//加载数据动画
mAdapter.openLoadAnimation(new ScaleInAnimation());
mRecyclerView.setAdapter(mAdapter);
//监听recyclerview滑动
//上拉刷新
mRecyclerView.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh() {
mAdapter.getPageBean().setRefresh(true);
loadData();//获取数据
}
});
//下拉加载更多
mRecyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore(View loadMoreView) {
mRecyclerView.setLoadMoreStatus(LoadMoreFooterView.Status.LOADING);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mAdapter.getPageBean().setRefresh(false);
loadData();;//获取数据
}
}, 1000);
}
});
//其中PageBean()中封装了与分页相关的数据(page总页数 rows 每页的行数
//totalCount 总记录数 //totalPage 总页数 //refresh 标志,是否刷新)
- 适配数据
public void GetHouseList(List<HouseItem> houseItemsList,PageBean pageBean) {
if (mAdapter.getPageBean().isRefresh()) {
mRecyclerView.setRefreshing(false);
mAdapter.reset(houseItemsList);
} else {
if(pageBean.getPage()==1){
mAdapter.reset(houseItemsList);
}else{
mAdapter.addAll(houseItemsList);
mRecyclerView.setLoadMoreStatus(LoadMoreFooterView.Status.GONE);
}
}
}
Step 5. 新建适配器adapter 让其继承自BaseReclyerViewAdapter<T>,其中T为我们要呈现数据的数据类型(结构)
public class search_family_adapter extends BaseReclyerViewAdapter<FamilyItem> {
public static final int ITEM_VIEW_TYPE_DEFAULT = 0;
public static final int ITEM_VIEW_TYPE_IMAGE = 1;
public static final int ITEM_VIEW_TYPE_URL = 2;
private Context mContext;
private FamilyPresenter mPresenter;
public search_family_adapter(Context context, FamilyPresenter mPresenter) {
super(context);
this.mContext=context;
this.mPresenter=mPresenter;
}
@Override
public int getItemViewType(int position) {
return getData().get(position).getType();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return search_Family_ViewHolder.create(mContext,viewType);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
super.onBindViewHolder(holder, position);
if(holder instanceof search_Family_ViewHolder) {
((search_Family_ViewHolder) holder).setData(mPresenter, get(position), position);
}
}
@Override
public int getItemCount() {
return getData().size();
}
}
Step 6. 新建适配器adapter 新建viewHolder(让数据的每一项适配viewItem),让其继承自RecyclerView.ViewHolder
public class search_Family_ViewHolder extends RecyclerView.ViewHolder {
private Context mContext;
private FamilyPresenter mPresenter;
private FamilyItem familyItem;
private int position;
private View itemView;
private int type;
public ImageView familyIcoIv;
public TextView familyNameTv;
public TextView familyIDcardTv;
public ImageView deleteBtn;
public ImageView detailBtn;
public LinearLayout family_detail;
public LinearLayout locateIcon;
public ImageView locateBtn;
public static search_Family_ViewHolder create(Context context, int type) {
search_Family_ViewHolder imageViewHolder = new search_Family_ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_search_family, null), context, type);
return imageViewHolder;
}
public search_Family_ViewHolder(View itemView, final Context context, int type) {
super(itemView);
this.itemView=itemView;
this.type=type;
this.mContext = context;
initView();
}
//在Item项中通过Id查找到对应控件
private void initView()
familyIcoIv = (ImageView) itemView.findViewById(R.id.familyIco);
familyNameTv = (TextView) itemView.findViewById(R.id.familyName);
familyIDcardTv = (TextView) itemView.findViewById(R.id.familyIDcard);
}
//循环每一项数据,匹配到对应的控件,显示出来
public void setData(FamilyPresenter mPresenter2, FamilyItem familyItem2, final int position2) {
if(mPresenter2==null|| familyItem2 ==null){
return;
}
this.familyItem = familyItem2;
this.mPresenter=mPresenter2;
this.position=position2;
//头像
if(familyItem.getPhoto()!=null){
familyIcoIv.setImageResource(familyItem.getFamilyheader);
familyNameTv.setText(familyItem.getPopulationame());
familyIDcardTv.setText(familyItem.getIdentitynumber());
//处理Item项或者Item项中控件的的单击等事件
detailBtn = (ImageView) itemView.findViewById(R.id.detailBtn);
detailBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//详情
if (mPresenter != null) {
mPresenter.detailFamily(familyItem.getId(), position);
}
}
});
family_detail = (LinearLayout) itemView.findViewById(R.id.family_detail);
family_detail.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//详情
if (mPresenter != null) {
mPresenter.detailFamily(familyItem.getId(), position);
}
}
});
}
}
How to use 2 (Viewholder通用)
Step 1. 在布局文件中引用IRecyclerView:(和上同)
Step 2. 新建Item项的布局文件(和上同)
Step 3. 在要使用Irecycleview 的Activity中使用:
- 初始化:
mRecyclerView = (IRecyclerView) findViewById(R.id.family_recycler_view);
LinearLayoutManager pLayoutManager= new LinearLayoutManager(this);
mLayoutManager=pLayoutManager;
// 设置布局管理器
mRecyclerView.setLayoutManager(mLayoutManager);
// 设置adapter
//mAdapter = new search_family_adapter(this, familyPresenterImpl);
mAdapter = new search_family_adapter(this,R.layout.item_search_family);
mAdapter.openLoadAnimation(new ScaleInAnimation());
mRecyclerView.setAdapter(mAdapter);
//监听recyclerview滑动
setViewTreeObserver();
//上拉刷新
mRecyclerView.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh() {
mAdapter.getPageBean().setRefresh(true);
loadData();
}
});
//下拉加载更多
mRecyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore(View loadMoreView) {
mRecyclerView.setLoadMoreStatus(LoadMoreFooterView.Status.LOADING);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mAdapter.getPageBean().setRefresh(false);
loadData();
}
}, 1000);
}
});
- 适配数据(和上同):
public void GetFamilyList(List<FamilyItem> playBeanListHolderList, PageBean pageBean) {
if (mAdapter.getPageBean().isRefresh()) {
mRecyclerView.setRefreshing(false);
mAdapter.replaceAll(playBeanListHolderList);
} else {
if(pageBean.getPage()==1){
mAdapter.replaceAll(playBeanListHolderList);
}else{
mAdapter.addAll(playBeanListHolderList);
mRecyclerView.setLoadMoreStatus(LoadMoreFooterView.Status.GONE);
}
}
//判断是否还可以加载更多
if (pageBean.getTotalPage() <= pageBean.getPage()) {
mRecyclerView.setLoadMoreStatus(LoadMoreFooterView.Status.THE_END);
}
mAdapter.setOnItemClickListener(new search_family_adapter.OnItemClickListener() {
@Override
public void onItemClick(View view, BigDecimal id,int position) {
//...
}
});
mAdapter.setOnLocateClickListener(new search_family_adapter.OnLocateBtnClickListener() {
@Override
public void onLocateBtnClick(View view,String qrcode, int position) {
//...
}
});
}
Step 4. 新建适配器adapter让其继承CommonRecycleViewAdapter<T>
public class search_family_adapter extends CommonRecycleViewAdapter<FamilyItem> {
public search_family_adapter(Context context, int layoutId) {
super(context, layoutId);
}
@Override
public void convert(ViewHolderHelper helper, FamilyItem familyItem) {
//头像
if(familyItem.getPhoto()!=null){
String URL= PictureURLService.getFamiyPictureURL(familyItem.getPhoto());
helper.setBigImageUrl(R.id.familyIco,URL);
}else {
helper.setImageResource(R.id.familyIco,R.mipmap.ic_familyheader);
}
helper.setText(R.id.familyName,familyItem.getPopulationame());
helper.setText(R.id.familyIDcard,familyItem.getIdentitynumber());
handleOnClick(helper,familyItem);
LocatedOnClick(helper,familyItem);
}
private OnItemClickListener mOnItemClickListener;
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
mOnItemClickListener = onItemClickListener;
}
public interface OnItemClickListener {
void onItemClick(View view, BigDecimal id, int position);
}
private OnLocateBtnClickListener mBtnLocateClickListener;
public void setOnLocateClickListener(OnLocateBtnClickListener onLocateClickListener) {
mBtnLocateClickListener = onLocateClickListener;
}
public interface OnLocateBtnClickListener {
void onLocateBtnClick(View view,String qrcode, int position);
}
private void handleOnClick(final ViewHolderHelper helper,final FamilyItem familyItem) {
if (mOnItemClickListener != null) {
helper.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mOnItemClickListener.onItemClick(v,familyItem.getId(), helper.getLayoutPosition());
}
});
helper.setOnClickListener(R.id.detailBtn, new View.OnClickListener() {
@Override
public void onClick(View v) {
mOnItemClickListener.onItemClick(v,familyItem.getId(), helper.getLayoutPosition());
}
});
}
}
private void LocatedOnClick(final ViewHolderHelper helper,final FamilyItem familyItem) {
if (mBtnLocateClickListener != null) {
helper.setOnClickListener(R.id.locateBtn, new View.OnClickListener() {
@Override
public void onClick(View v) {
mBtnLocateClickListener.onLocateBtnClick(v,familyItem.getQrcode(), helper.getLayoutPosition());
}
});
helper.setOnClickListener(R.id.locateIcon, new View.OnClickListener() {
@Override
public void onClick(View v) {
mBtnLocateClickListener.onLocateBtnClick(v,familyItem.getQrcode(), helper.getLayoutPosition());
}
});
}
}
}
Step6. ViewHold使用通用ViewHolderHelper
二、 多图片选择器ImagePicker库
完全仿微信的图片选择,并且提供了多种图片加载接口,选择图片后可以旋转,可以裁剪成矩形
How to use
Step 1. 在自己项目中添加本项目依赖
compile 'com.lqr.imagepicker:library:1.0.0'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.3'
Step2. 实现ImageLoader接口,实现图片加载策略
public class UILImageLoader implements com.lqr.imagepicker.loader.ImageLoader {
@Override
public void displayImage(Activity activity, String path, ImageView imageView, int width, int height) {
ImageSize size = new ImageSize(width, height);
com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(Uri.parse("file://" + path).toString(), imageView, size);
}
@Override
public void clearMemoryCache() {
}
}
Step3. Application中初始化
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
initUniversalImageLoader();
initImagePicker();
}
private void initUniversalImageLoader() {
//初始化ImageLoader
ImageLoader.getInstance().init(
ImageLoaderConfiguration.createDefault(getApplicationContext()));
}
/**
* 初始化仿微信控件ImagePicker
*/
private void initImagePicker() {
ImagePicker imagePicker = ImagePicker.getInstance();
imagePicker.setImageLoader(new UILImageLoader()); //设置图片加载器
imagePicker.setShowCamera(true); //显示拍照按钮
imagePicker.setCrop(true); //允许裁剪(单选才有效)
imagePicker.setSaveRectangle(true); //是否按矩形区域保存
imagePicker.setSelectLimit(9); //选中数量限制
imagePicker.setStyle(CropImageView.Style.RECTANGLE); //裁剪框的形状
imagePicker.setFocusWidth(800); //裁剪框的宽度。单位像素(圆形自动取宽高最小值)
imagePicker.setFocusHeight(800); //裁剪框的高度。单位像素(圆形自动取宽高最小值)
imagePicker.setOutPutX(1000);//保存文件的宽度。单位像素
imagePicker.setOutPutY(1000);//保存文件的高度。单位像素
}
}
Step4. 图片选择界面代码:
public static final int IMAGE_PICKER = 100;
Intent intent = new Intent(this, ImageGridActivity.class);
startActivityForResult(intent, IMAGE_PICKER);
5、获取所选图片信息:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {//返回多张照片
if (data != null) {
boolean isOrig = data.getBooleanExtra(ImagePreviewActivity.ISORIGIN, false);
ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
}
}
}
}
三、 扫一扫:简单的ZXing使用
ZXing是Google提供的条形码、二维码等的生成、解析的库
How to use
Step1. 添加本项目依赖
compile 'com.google.zxing:core:3.2.1'
compile 'cn.bingoogolapple:bga-qrcodecore:1.1.7@aar'
compile 'cn.bingoogolapple:bga-zxing:1.1.7@aar'
Step2. 创建 Scan.xml布局文件,这个就是扫描二维码的界面
android:id="@+id/zxingview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:qrcv_animTime="1000"
app:qrcv_barCodeTipText="将条码放入框内,即可自动扫描"
app:qrcv_barcodeRectHeight="140dp"
app:qrcv_borderColor="@android:color/white"
app:qrcv_borderSize="0.5dp"
app:qrcv_cornerColor="@color/green3"
app:qrcv_cornerLength="20dp"
app:qrcv_cornerSize="3dp"
app:qrcv_isBarcode="false"
app:qrcv_isCenterVertical="false"
app:qrcv_isOnlyDecodeScanBoxArea="false"
app:qrcv_isScanLineReverse="true"
app:qrcv_isShowDefaultGridScanLineDrawable="false"
app:qrcv_isShowDefaultScanLineDrawable="true"
app:qrcv_isShowTipBackground="true"
app:qrcv_isShowTipTextAsSingleLine="false"
app:qrcv_isTipTextBelowRect="true"
app:qrcv_maskColor="#33FFFFFF"
app:qrcv_qrCodeTipText="将二维码/条码放入框内,即可自动扫描"
app:qrcv_rectWidth="200dp"
app:qrcv_scanLineColor="@color/green1"
app:qrcv_scanLineMargin="0dp"
app:qrcv_scanLineSize="0.5dp"
app:qrcv_tipTextColor="@android:color/white"
app:qrcv_tipTextSize="12sp"
app:qrcv_toolbarHeight="56dp"
app:qrcv_topOffset="90dp"/>
Step3. 扫描二维码的Activity页面
public class ScanActivity extends BaseActivity implements QRCodeView.Delegate {
public final static int result_Code=99;
private FrameLayout mView;
private PopupWindow mPopupWindow;
public static final int IMAGE_PICKER = 100;
ZXingView mZxingview;
public void initListener() {
mZxingview.setDelegate(this);}
@Override
protected void onStart() {
setContentView(R.layout.activity_scan);
super.onStart();
mZxingview=(ZXingView) findViewById(R.id.zxingview);
mZxingview.setDelegate(this);
mZxingview.startCamera();
mZxingview.startSpot();
mLlFanyi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(ScanActivity.this, ImageGridActivity.class);
startActivityForResult(intent, IMAGE_PICKER);
}});
}
@Override
protected void onStop() {
super.onStop();
mZxingview.stopCamera();
}
@Override
protected void onDestroy() {
super.onDestroy();
mZxingview.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(this).inflate(R.menu.menu_more, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onScanQRCodeSuccess(String result) {
handleResult(result);
}
private void vibrate() {
Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
vibrator.vibrate(200);
}
public void onScanQRCodeOpenCameraError() {
showToast("打开相机出错");
}
private void handleResult(String result) {
vibrate();
Intent intent =new Intent();
//设置返回信息
intent.putExtra("qrcode", result);
//设置返回码和通信使者
setResult(result_Code,intent);
//完成,关闭当前页面并返回
finish();
}
}
四、 CityPickerView 实现省市区三级联动选择
需要在地址选择中实现省市区三级联动,方便用户快速的填写地址
How to use
Step1. 添加本项目依赖
compile 'liji.library.dev:citypickerview:0.7.0'
Step2. Activity使用
//Texview的点击事件
public void chooseArea(View view) {
//判断输入法的隐藏状态
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) {
imm.hideSoftInputFromWindow(view.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
selectAddress();//调用CityPicker选取区域
}
}
private void selectAddress() {
CityPicker cityPicker = new CityPicker.Builder(NewAddressActivity.this)
.textSize(14)
.title("地址选择")
.titleBackgroundColor("#FFFFFF")
.titleTextColor("#696969")
.confirTextColor("#696969")
.cancelTextColor("#696969")
.province("新疆省")
.city("阿克苏市")
.district("温宿县")
.textColor(Color.parseColor("#000000"))
.provinceCyclic(true)
.cityCyclic(false)
.districtCyclic(false)
.visibleItemsCount(7)
.itemPadding(10)
.onlyShowProvinceAndCity(false)
.build();
cityPicker.show();
//监听方法,获取选择结果
cityPicker.setOnCityItemClickListener(new CityPicker.OnCityItemClickListener() {
@Override
public void onSelected(String... citySelected) {
//省份
String province = citySelected[0];
//城市
String city = citySelected[1];
//区县(如果设定了两级联动,那么该项返回空)
String district = citySelected[2];
//邮编
String code = citySelected[3];
//为TextView赋值
new_address_area.setText(province.trim() + "-" + city.trim() + "-" + district.trim());
}
});
}
五、 AlertDialod
弹出确定删除(一般删除数据时,我们一般都要弹出确定删除弹出框,以避免数据的误删除)
How to use
Step1. 新建MDAlertDialog
public class MDAlertDialog implements View.OnClickListener {
private Dialog mDialog;
private View mDialogView;
private TextView mTitle;
private TextView mContent;
private TextView mLeftBtn;
private TextView mRightBtn;
private static Context mContext;
private Builder mBuilder;
public MDAlertDialog(Builder builder) {
mBuilder = builder;
mDialog = new Dialog(mContext, R.style.MyDialogStyle);
mDialogView = View.inflate(mContext, R.layout.widget_md_dialog, null);
mTitle = (TextView) mDialogView.findViewById(R.id.md_dialog_title);
mContent = (TextView) mDialogView.findViewById(R.id.md_dialog_content);
mLeftBtn = (TextView) mDialogView.findViewById(R.id.md_dialog_leftbtn);
mRightBtn = (TextView) mDialogView.findViewById(R.id.md_dialog_rightbtn);
mDialogView.setMinimumHeight((int) (ScreenSizeUtils.getInstance(mContext).getScreenHeight
() * builder.getHeight()));
mDialog.setContentView(mDialogView);
Window dialogWindow = mDialog.getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
lp.width = (int) (ScreenSizeUtils.getInstance(mContext).getScreenWidth() * builder.getWidth());
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity = Gravity.CENTER;
dialogWindow.setAttributes(lp);
initDialog();
}
private void initDialog() {
mDialog.setCanceledOnTouchOutside(mBuilder.isTouchOutside());
if (mBuilder.getTitleVisible()) {
mTitle.setVisibility(View.VISIBLE);
} else {
mTitle.setVisibility(View.GONE);
}
mTitle.setText(mBuilder.getTitleText());
mTitle.setTextColor(mBuilder.getTitleTextColor());
mTitle.setTextSize(mBuilder.getTitleTextSize());
mContent.setText(mBuilder.getContentText());
mContent.setTextColor(mBuilder.getContentTextColor());
mContent.setTextSize(mBuilder.getContentTextSize());
mLeftBtn.setText(mBuilder.getLeftButtonText());
mLeftBtn.setTextColor(mBuilder.getLeftButtonTextColor());
mLeftBtn.setTextSize(mBuilder.getButtonTextSize());
mRightBtn.setText(mBuilder.getRightButtonText());
mRightBtn.setTextColor(mBuilder.getRightButtonTextColor());
mRightBtn.setTextSize(mBuilder.getButtonTextSize());
mLeftBtn.setOnClickListener(this);
mRightBtn.setOnClickListener(this);
}
public void show() {
mDialog.show();}
public void dismiss() {
mDialog.dismiss();}
@Override
public void onClick(View view) {
int i = view.getId();
if (i == R.id.md_dialog_leftbtn && mBuilder.getListener() != null) {
mBuilder.getListener().clickLeftButton(mLeftBtn);
return;
}
if (i == R.id.md_dialog_rightbtn && mBuilder.getListener() != null) {
mBuilder.getListener().clickRightButton(mRightBtn);
return;
}
}
public static class Builder {
private String titleText;
private int titleTextColor;
private int titleTextSize;
private String contentText;
private int contentTextColor;
private int contentTextSize;
private String leftButtonText;
private int leftButtonTextColor;
private String rightButtonText;
private int rightButtonTextColor;
private int buttonTextSize;
private boolean isTitleVisible;
private boolean isTouchOutside;
private float height;
private float width;
private DialogOnClickListener listener;
public Builder(Context context) {
mContext = context;
titleText = "提示";
titleTextColor = ContextCompat.getColor(mContext, R.color.black_light);
contentText = "";
contentTextColor = ContextCompat.getColor(mContext, R.color.black_light);
leftButtonText = "取消";
leftButtonTextColor = ContextCompat.getColor(mContext, R.color.black_light);
rightButtonText = "确定";
rightButtonTextColor = ContextCompat.getColor(mContext, R.color.black_light);
listener = null;
isTitleVisible = true;
isTouchOutside = true;
height = 0.21f;
width = 0.73f;
titleTextSize = 16;
contentTextSize = 14;
buttonTextSize = 14;
}
public String getTitleText() {
return titleText;
}
public Builder setTitleText(String titleText) {
this.titleText = titleText;
return this;
}
public int getTitleTextColor() {
return titleTextColor;
}
public Builder setTitleTextColor(@ColorRes int titleTextColor) {
this.titleTextColor = ContextCompat.getColor(mContext, titleTextColor);
return this;
}
public String getContentText() {
return contentText;
}
public Builder setContentText(String contentText) {
this.contentText = contentText;
return this;
}
public int getContentTextColor() {
return contentTextColor;
}
public Builder setContentTextColor(@ColorRes int contentTextColor) {
this.contentTextColor = ContextCompat.getColor(mContext, contentTextColor);
return this;
}
public String getLeftButtonText() {
return leftButtonText;
}
public Builder setLeftButtonText(String leftButtonText) {
this.leftButtonText = leftButtonText;
return this;
}
public int getLeftButtonTextColor() {
return leftButtonTextColor;
}
public Builder setLeftButtonTextColor(@ColorRes int leftButtonTextColor) {
this.leftButtonTextColor = ContextCompat.getColor(mContext, leftButtonTextColor);
return this;
}
public String getRightButtonText() {
return rightButtonText;
}
public Builder setRightButtonText(String rightButtonText) {
this.rightButtonText = rightButtonText;
return this;
}
public int getRightButtonTextColor() {
return rightButtonTextColor;
}
public Builder setRightButtonTextColor(@ColorRes int rightButtonTextColor) {
this.rightButtonTextColor = ContextCompat.getColor(mContext, rightButtonTextColor);
return this;
}
public boolean getTitleVisible() {
return isTitleVisible;
}
public Builder setTitleVisible(boolean titleVisible) {
isTitleVisible = titleVisible;
return this;
}
public boolean isTouchOutside() {
return isTouchOutside;
}
public Builder setCanceledOnTouchOutside(boolean touchOutside) {
isTouchOutside = touchOutside;
return this;
}
public float getHeight() {
return height;
}
public Builder setHeight(float height) {
this.height = height;
return this;
}
public float getWidth() {
return width;
}
public Builder setWidth(float width) {
this.width = width;
return this;
}
public int getContentTextSize() {
return contentTextSize;
}
public Builder setContentTextSize(int contentTextSize) {
this.contentTextSize = contentTextSize;
return this;
}
public int getTitleTextSize() {
return titleTextSize;
}
public Builder setTitleTextSize(int titleTextSize) {
this.titleTextSize = titleTextSize;
return this;
}
public int getButtonTextSize() {
return buttonTextSize;
}
public Builder setButtonTextSize(int buttonTextSize) {
this.buttonTextSize = buttonTextSize;
return this;
}
public DialogOnClickListener getListener() {
return listener;
}
public Builder setOnclickListener(DialogOnClickListener listener) {
this.listener = listener;
return this;
}
public MDAlertDialog build() {
return new MDAlertDialog(this);
}
}
Step2. 在要控制的presenter层加如下代码
public void deletePopulation(final BigDecimal id, final int position) {
mdAlertDialog = new MDAlertDialog.Builder((Context) mPresenterView)
.setHeight(0.20f) //屏幕高度*0.3
.setWidth(0.6f) //屏幕宽度*0.7
.setTitleVisible(true)
.setTitleText("提示")
.setTitleTextColor(R.color.black_light)
.setContentText("确定删除该条记录吗?")
.setContentTextColor(R.color.black_light)
.setLeftButtonText("不删除")
.setLeftButtonTextColor(R.color.black_light)
.setRightButtonText("删除")
.setRightButtonTextColor(R.color.gray)
.setTitleTextSize(16)
.setContentTextSize(14)
.setButtonTextSize(14)
.setOnclickListener(new DialogOnClickListener() {
@Override
public void clickLeftButton(View view) {
mdAlertDialog.dismiss();
}
@Override
public void clickRightButton(View view) {
mdAlertDialog.dismiss();
//mPresenterView.startProgressDialog();
Subscription subscription = mPopManageInteractor.deleteById(new IGetDataDelegate<String>() {
@Override
public void getDataSuccess(String deleteflag) {
mPresenterView.deletePopulationItem(deleteflag,position);
}
@Override
public void getDataError(String errmsg) {
mPresenterView.deletePopulationItem(errmsg,position);
}
},id);
mSubscriptions.add(subscription);
}
})
.build();
mdAlertDialog.show();
}
六、 基础工具 librays/utils
librays/utils/date DateUtil 日期时间的处理转化类
- 获取SimpleDateFormat getDateFormat(String pattern)
- 获取日期中的某数值。如获取月份 getInteger(Date date, int dateType)
- 增加日期中某类型的某数值。如增加日期 addInteger(Date date, int dateType, int amount)
- 获取精确的日期 getAccurateDate(List<Long> timestamps)
- 判断字符串是否为日期字符串 isDate(String date)
- 获取日期字符串的日期风格。失敗返回null getDateStyle(String date)
- 将日期字符串转化为日期。失败返回null StringToDate(String date) StringToDate(String date, String pattern)
StringToDate(String date, DateStyle dateStyle) - 将日期转化为日期字符串 DateToString(Date date, String pattern) DateToString(Date date, DateStyle dateStyle)
- 将日期字符串转化为另一日期字符串 StringToString(String date, String newPattern) StringToString(String date, DateStyle newDateStyle)
StringToString(String date, String olddPattern, DateStyle newDateStyle) StringToString(String date, DateStyle olddDteStyle, DateStyle newDateStyle) - 增加日期的年份 addYear(String date, int yearAmount) addYear(Date date, int yearAmount)
- 增加日期的月份 addYear(Date date, int yearAmount) addMonth(String date, int monthAmount)
- 增加日期的天数 addDay(String date, int dayAmount) addDay(Date date, int dayAmount)
- 增加日期的小时 addHour(String date, int hourAmount) addHour(Date date, int hourAmount)
- 增加日期的分钟 addMinute(String date, int minuteAmount) addMinute(Date date, int minuteAmount)
- 增加日期的秒钟 addSecond(String date, int secondAmount) addSecond(Date date, int secondAmount)
- 获取日期的年份\月份\天数\小时\分钟\秒钟 getYear\ getMonth\getDay...
- 获取日期 (默认yyyy-MM-dd格式) getDate(String date) getDate(Date date)
- 获取日期的时间(默认HH:mm:ss格式) getTime(String date) getTime(Date date)
- 获取日期的星期 getWeek(String date) getWeek(Date date)
- 获取两个日期相差的天数 getIntervalDays(String date, String otherDate) getIntervalDays(Date date, Date otherDate)
librays/utils/screen ScreenUtil 屏幕处理、尺寸相关的转化类:
- 获得屏幕高度 getScreenWidth(Context context)
- 获得屏幕宽度 getScreenHeight(Context context)
- 获得状态栏的高度 getStatusHeight(Context context)
- 获取当前屏幕截图,包含状态栏 snapShotWithStatusBar(Activity activity)
- 获取当前屏幕截图,不包含状态栏 snapShotWithoutStatusBar(Activity activity)
- 将px值转换为sp值,保证文字大小不变 px2sp(float pxValue)
- 将sp值转换为px值,保证文字大小不变 sp2px(float spValue)
- 将px值转换为dip或dp值,保证尺寸大小不变 px2dp(float pxValue)
- 将dip或dp值转换为px值,保证尺寸大小不变 dp2px(float dipValue)
librays/utils/security
AESUtil AES加解密算法:
- 加密 Encrypt(String sSrc)
- 解密 Decrypt(String sSrc)
- discardNonBase64Bytes(byte[] data)
- discardNonBase64Chars(String data)
- isValidBase64Byte(byte b)
Base64 加密、解密算法:
- 加密 encode(byte[] data)
- 解密 decode(byte[] data) decode(String data)
- discardNonBase64Bytes(byte[] data)
CodeSecurity 加解密方法的封装类:
- desEncode desEncode(String str)
- desDecodeInfo desDecodeInfo(String str)
- AES解码 aesDecodeInfo(String str)
- AES编码 aesEncode(String str)
DESBase64Util 客户端与服务器通讯时对消息体加密和解密的工具类
- 先对消息体进行DES编码再进行BASE64编码 encodeInfo(String info)
- 先对消息体进行BASE64解码再进行DES解码 decodeInfo(String info)
How to use
- 保证服务器端和客户端的DESBase64Util.KEY 的值必须相同
- 信息编码。首先将要编码的对象转为string,然后在调用encodeInfo(String info)方法
String populationJson= gson.toJson(population);//population为实体类的变量
String desPopulationJson = DESBase64Util.encodeInfo(population_json);
JsonObject obj_contentpersons=new JsonObject();
obj_contentpersons.addProperty("des",desPopulationJson);
obj_contentpersons..toString();
- 信息解码。同1类似。获取des的值,调用encodeInfo()方法
Md5Security MD5加密类
- md5加密 getMD5(String info)
librays/common/FormatUtil 格式验证类:
- 验证手机号码格式(第一位必定为1,第二位必定为3或5或8或7,其他位置的可以为0-9) isMobileNO(String mobiles)
- 判断email格式是否正确 isEmail(String email)
- 判断是否全是数字 isNumeric(String str)
- 判断身份证格式 isIdCardNo(String idNum)
- 判定输入汉字 isChinese(char c)
- 检测String是否全是中文 checkNameChese(String name)
- 判断是否是银行卡号 checkBankCard(String cardId)
- 身份证的有效验证 IDCardValidate(String IDStr)
- 设置地区编码 GetAreaCode()
- 验证日期字符串是否是YYYY-MM-DD格式 isDataFormat(String str)
librays/common/GlideCircleTransformUtil glide转圆形图片类
librays/common/GlideRoundTransformUtil glide转换圆角图片
librays/common/ImageLoaderUtils 图片加载工具类 使用glide框架封装
- 图片加载 display(Context context, ImageView imageView, String url, int placeholder, int error)
display(Context context, ImageView imageView, String url)
display(Context context, ImageView imageView, File url)
display(Context context, ImageView imageView, int url)`
- 加载Small图片 displaySmallPhoto(Context context, ImageView imageView, String url)
- 加载Big图片 displayBigPhoto(Context context, ImageView imageView, String url)
- 加载圆形图片 displayRound(Context context, ImageView imageView, String url)
librays/common/IpUntils 获取Ip类
- 获取ip地址 GetHostIp()
librays/common/JsonUtils 与Json相关的数据转换类
- 将对象转为JSON串 toJson(Object src)
- 将JSON串转为对象(此方法不可用来转带泛型的集合) fromJson(String json, Class<T> classOfT)
- 将JSON串转为对象(此方法可用来转带泛型的集合) fromJson(String json, Type typeOfT)
- 获取json中的某个值 getValue(String json, String key)
- 获取json中的list值\Double值\Int值 getListValue(String json)\getDoubleValue(String json, String key)\getIntValue(String json, String key)
librays/common/KeyBordUtils 控制显示和隐藏软键盘
- 显示和隐藏软键盘 popSoftKeyboard(Context context, View view,boolean isShow)
- 显示软键盘 showSoftKeyboard(View view)
- 隐藏软键盘 hideSoftKeyboard(View view)
librays/common/NetWorkUntils 网络管理工具
- 检查网络是否可用 isNetConnected(Context paramContext)
- 检测wifi是否连接 isWifiConnected(Context context)
- 检测3G是否连接 is3gConnected(Context context)
- 判断网址是否有效 isLinkAvailable(String link)
librays/common/TimeUtil 时间、日期处理类:
- 时间戳转特定格式时间 formatData(String dataFormat, long timeStamp)
- 将毫秒转换成秒 convertToSecond(Long time)
- String类型的日期时间转化为Date类型 getDateByFormat
- 获取偏移之后的Date getDateByOffset(Date date, int calendarField, int offset)
- 获取指定日期时间的字符串(可偏移) getStringByOffset(String strDate, String format, int calendarField, int offset)
- Date类型转化为String类型(可偏移) getStringByOffset(Date date, String format, int calendarField, int offset)
- from yyyy-MM-dd HH:mm:ss to MM-dd HH:mm formatDate(String before)
- Date类型转化为String类型 getStringByFormat(Date date, String format)
- 获取指定日期时间的字符串(用于导出想要的格式) getStringByFormat(String strDate, String format)
- 获取milliseconds表示的日期时间的字符串 getStringByFormat(long milliseconds, String format)
- 获取表示当前日期时间的字符串 getCurrentDate(String format)
- 获取当前系统当天日期 getCurrentDay() getCurrentDay2()
- 获取当前系统前后第几天 getNextDay(int i)
- 获取当前系统前后第几小时 getNextHour(int i)
- 获取表示当前日期时间的字符串(可偏移) getCurrentDateByOffset(String format, int calendarField, int offset)
- 计算两个日期所差的天数 getOffectDay(long date1, long date2)
- 计算两个日期所差的小时数 getOffectHour(long date1, long date2)
- 计算两个日期所差的分钟数 getOffectMinutes(long date1, long date2)
- 获取本周一\获取本周日\获取本周的某一天 getFirstDayOfWeek(String format)\getLastDayOfWeek(String format)\getDayOfWeek(String format, int calendarField)
- 获取本月第一天\获取本月最后一天 getFirstDayOfMonth(String format)\getLastDayOfMonth(String format)
- 获取表示当前日期的0点时间毫秒数\获取表示当前日期24点时间毫秒数 getFirstTimeOfDay()\getLastTimeOfDay()
- 判断是否是闰年 isLeapYear(int year)
- 取指定日期为星期几 getWeekNumber(String strDate, String inFormat)
- 将字符串转换日期类型 toDate(String sdate)
- 距离当前多少个小时 getExpiredHour(String dateStr) getExpiredHour2(String dateStr)
- 判断给定字符串时间是否为今日 isToday(String sdate)
- 根据用户生日计算年龄 getAgeByBirthday(Date birthday)
- 通过日期来确定星座 getStarSeat(int mouth, int day)
- 返回聊天时间 getChatTimeForShow(long time)
- 获取指定时间的毫秒值 getDatelongMills(String fomat, String dateStr)
- 两个日期比较 compare_date(String DATE1, String DATE2)
librays/common/Toast统一管理类:
- 短时间显示Toast showShort(CharSequence message)
showShort(int strResId)- 长时间显示Toast showLong(CharSequence message)
showLong(int strResId)
- 自定义显示Toast时间 show(CharSequence message, int duration)
show(Context context, int strResId, int duration) - 显示有image的toast showToastWithImg(final String tvStr, final int imageResource)
librays/utils/compressorutils/Compressor 压缩图片类
- 图片压缩为File型 compressToFile(File file)
- 图片压缩为Bitmap型 compressToBitmap(File file)
- 图片压缩为Observable<File>型 compressToFileAsObservable(final File file)
- 图片压缩为Observable<Bitmap>型 compressToBitmapAsObservable(final File file)
librays/utils/compressorutils/FileUtil 文件处理类
- 获取文件名 splitFileName(String fileName)
- 根据uri获取文件路径 getFileName(Context context, Uri uri)
- 根据uri获取真文件路径 getRealPathFromURI(Context context, Uri contentUri)
- 文件重命名 rename(File file, String newName)
- 文件copy copy(InputStream input, OutputStream output)
- copyLarge copyLarge(InputStream input, OutputStream output) copyLarge(InputStream input, OutputStream output, byte[] buffer)
- 判断文件是否存在 fileIsExists(String path)
librays/utils/compressorutils/ImageUtil 图像类
- 获取压缩后bitmap getScaledBitmap(Context context, Uri imageUri, float maxWidth, float maxHeight)
- 压缩图片 compressImage(Context context, Uri imageUri, float maxWidth, float maxHeight,
Bitmap.CompressFormat compressFormat, int quality, String parentPath) - generateFilePath(Context context, String parentPath, Uri uri, String extension)
- calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight)
librarys/utils/SPUtils/ ----SharedPreferences文件的操作类
SharedPreferences sharedPreferences = context.getSharedPreferences(SHARE_FILE_NAME, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(SHARE_VALUES_CONTENT, null);
- SP中写入String类型value ----putString(String key, String value)
- SP中写入String类型value ----putString(String key, String value)
- SP中读取String ----getString(String key)
- SP中写入int类型value ---putInt(String key, int value)
- SP中读取int ---getInt(String key)
- SP中写入long类型value ---putLong(String key, long value)
- SP中读取long ----getLong(String key)
- SP中写入float类型value ----putFloat(String key, float value)
- SP中读取float ----getFloat(String key)
- SP中写入boolean类型value----putBoolean(String key, boolean value)
- SP中读取boolean ----getBoolean(String key)
- 获取SP中所有键值对 ----getAll()
- 从SP中移除该key ----remove(String key)
- 判断SP中是否存在该key ----contains(String key)
七、主页面的底部tab切换组件 FlycoTabLayout
Gradle
dependencies{
compile 'com.android.support:support-v4:25.3.0'
compile 'com.flyco.tablayout:FlycoTabLayout_Lib:2.1.2@aar'
}
How to use
layout文件引用
<com.flyco.tablayout.CommonTabLayout
android:id="@+id/tab_layout"
android:background="@color/gray4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:layout_alignParentBottom="true"
tl:tl_indicator_height="0dp"
tl:tl_textSelectColor="@color/assist_blue_down"
tl:tl_textUnselectColor="@color/gray0"
tl:tl_iconHeight="30dp"
tl:tl_iconWidth="30dp"
tl:tl_textsize="12sp"
tl:tl_underline_height="1dp" />
activity赋值
private CommonTabLayout tabLayout;
private String[] mTitles = {"首页", "地图","访民情","我的"};///
//"首页", "地图","访民情","我的" 显示的图标
private int[] mIconUnselectIds = {
R.mipmap.index_icon_bottom_1a,R.mipmap.index_icon_bottom_2a,R.mipmap.index_icon_bottom_3a,R.mipmap.index_icon_bottom_4a};
//"首页", "地图","访民情","我的" 单击时显示的图标
private int[] mIconSelectIds = {
R.mipmap.index_icon_bottom_1b,R.mipmap.index_icon_bottom_2b, R.mipmap.index_icon_bottom_3b,R.mipmap.index_icon_bottom_4b};
private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
tabLayout= (CommonTabLayout) findViewById(R.id.tab_layout);
tabLayout.measure(0,0);
tabLayoutHeight=tabLayout.getMeasuredHeight();
initTab()
}
/**
* 初始化tab
*/
private void initTab() {
for (int i = 0; i < mTitles.length; i++) {
mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
}
tabLayout.setTabData(mTabEntities);
//点击监听
tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
@Override
public void onTabSelect(int position) {
SwitchTo(position);
}
@Override
public void onTabReselect(int position) {
}
});
}
/**
* tab切换
*/
private void SwitchTo(int position) {
// LogUtils.logd("主页菜单position" + position);
switch (position) {
//首页
case 0:
break;
//地图
case 1:
break;
//访民情
case 2:
break;
//我的
case 3:
break;
default:
break;
}
}
八、语音识别工具 ----[科大讯飞] 例: 参考KEDAXUNFEI项目 官网
SVN:https://220.171.2.106:8800/svn/KEDAXUNFEI/
How to use
Step1. gradle
- 在官网申请服务获得Appid。(已获得Appid:5966ff6e)
- 右键单击main目录,选择New>Folder>Assets Folder ,将ifytek文件夹粘入该文件夹下
- 右键单击main目录,选择New>Directory>jinLibs,将armeabi和armeabi-v7a放入该文件夹下
- 右键单击app目录,选择New>Directory>libs,将Msc.jar放入该文件夹下,build.gradle的dependencies加入 compile files('libs/Msc.jar')
- 将DictationResult.java实体类文件放到项目library包下(DictationResult.java为语音识别后要接收数据的bean文件)
Step2. AndroidManifest引入如下权限
<!--连接网络权限,用于执行云端语音能力 -->
<uses-permission android:name="android.permission.INTERNET" />
<!--获取手机录音机使用权限,听写、识别、语义理解需要用到此权限 -->
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<!--读取网络信息状态 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!--获取当前wifi状态 -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!--允许程序改变网络连接状态 -->
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<!--读取手机信息权限 -->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Step3. 在application或activity的OnCreate函数初始化变量
// 语音配置对象初始化(如果只使用 语音识别 或 语音合成 时都得先初始化这个),将APPID改为自己申请的值
SpeechUtility.createUtility(MainActivity.this, SpeechConstant.APPID + "=578f1af7");
Step4. 单击button后,启动对话框开始录音
private RecognizerDialog iatDialog;
//etText为语音识别后的文字输出文本框
EditText etText = (EditText) findViewById(R.id.main_et_text);
//btnStartSpeak为单击后启动语音识别的按钮
Button btnStartSpeak== (Button) findViewById(R.id.main_btn_startSpeak);
//触犯录音按钮后执行单击事件
btnStartSpeak.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//在单击button后,启动对话框开始录音
iatDialog = new RecognizerDialog(MainActivity.this, mInitListener);
iatDialog.setListener(new RecognizerDialogListener() {
String resultJson = "[";//放置在外边做类的变量则报错,会造成json格式不对(?)
@Override
public void onResult(RecognizerResult recognizerResult, boolean isLast) {
System.out.println("----------------- onResult -----------------");
if (!isLast) {
resultJson += recognizerResult.getResultString() + ",";}
else { resultJson += recognizerResult.getResultString() + "]";}
if (isLast) {
//解析语音识别后返回的json格式的结果
Gson gson = new Gson();
List<DictationResult> resultList = gson.fromJson(resultJson,
new TypeToken<List<DictationResult>>() {}.getType());
String result = "";
for (int i = 0; i < resultList.size() - 1; i++) {
result += resultList.get(i).toString();
}
etText.setText(result);
//获取焦点
etText.requestFocus();
//将光标定位到文字最后,以便修改
etText.setSelection(result.length());
}
}
@Override
public void onError(SpeechError speechError) {
//自动生成的方法存根
speechError.getPlainDescription(true);
}
});
//开始听写,需将sdk中的assets文件下的文件夹拷入项目的assets文件夹下(没有的话自己新建)
iatDialog.show();
}
private InitListener mInitListener = new InitListener() {
@Override
public void onInit(int code) {
Log.d(TAG, "SpeechRecognizer init() code = " + code);
if (code != ErrorCode.SUCCESS) {
Toast.makeText(MainActivity.this, "初始化失败,错误码:" + code, Toast.LENGTH_SHORT).show();
}
}
};
九、 基础知识
文件按表单形式上传(Retrofit)
Step1. Retrofit APIService接口定义 方法
@Multipart
@POST("upload.action?commandId=PopulationManage&command=savemobile")
Observable<String> savepop(@Query("population") String pop ,@Part MultipartBody.Part file);
Step2. 调用Retrofit方法上传文件
File file_img = new File(images.get(0).path);
RequestBody requestFile = RequestBody.create(MediaType.parse("application/otcet-stream"), file_img);
MultipartBody.Part body body = MultipartBody.Part.createFormData("aFile", file_img.getName(), requestFile);
return RetrofitManager.getInstance()
.getLiveAPIService().savepop(population,body).
compose(TransformUtils.<String >defaultSchedulers())
.subscribe(new MSubscriber<String>() {
@Override
public void onError(Throwable e)
{
super.onError(e);
}
public void onNext(final String msg) {
try {
LogUtil.e("保存成功");
delegate.getDataSuccess(msg);
} catch (Exception e) {
LogUtil.e("数据有误" +e.toString());
}
}
})
获取Activity返回的参数
十、 TDevice net.oschina.app.util.TDevice(Oschina项目)
- 安装APK installAPK()
- 获取当前版本名称 getVersionName()
- 获取当前版本号 getVersionCode()
- 打开手机端的应用市场 openAppInMarket()
- 隐藏软键盘 hideSoftKeyboard()
- 显示软键盘 showSoftKeyboard()
- 判断是否为平板电脑 isTablet()
- 判断WIFI是否打开 isWifiOpen()
- 判断是否有WebView hasWebView()
十一、 Easypermissions(Oschina项目)
- 简书 Android开源项目-Easypermissions
-
Android权限最佳实践和代码实例分析
Easypermissions简化了Android M的运行时权限的申请、结果处理、判断等步骤.
How to use
Step1. Gradle
dependencies {
compile 'pub.devrel:easypermissions:0.1.9'
}
Step2. 检查权限
String[] perms = {Manifest.permission.CAMERA, Manifest.permission.CHANGE_WIFI_STATE};
if (EasyPermissions.hasPermissions(this, perms)) {
} else {
}
Step3. 申请权限
EasyPermissions.requestPermissions(this, "拍照需要摄像头权限",
RC_CAMERA_AND_WIFI, perms);
Step4. 实现EasyPermissions.PermissionCallbacks接口,直接处理权限是否成功申请
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// Forward results to EasyPermissions
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
//成功
@Override
public void onPermissionsGranted(int requestCode, List<String> list) {
// Some permissions have been granted
// ...
}
//失败
@Override
public void onPermissionsDenied(int requestCode, List<String> list) {
// Some permissions have been denied
// ...
}
十二、 版本检查更新 ----MainActivity的checkUpdate()方法(Oschina项目)
How to use
Step1. 引用
- CheckUpdateManager---net\oschina\app\improve\main\update\CheckUpdateManager
- DownloadService ---net\oschina\app\improve\main\update\DownloadService
Step2. 调用checkUpdateManger.checkUpdate()的方法,当获取的版本号大于当前版本时,调用RequestPermissions接口的call方法
Step3. checkUpdateManger.checkUpdate()需要请求服务以获取最新版本信息,需要根据具体情况修改。DownloadService也要做适当修改
@Override
protected void initData() {
super.initData();
checkUpdate();
}
private void checkUpdate() {
if (!AppContext.get(AppConfig.KEY_CHECK_UPDATE, true)) {
return;
}
CheckUpdateManager manager = new CheckUpdateManager(this, false);
manager.setCaller(this);
manager.checkUpdate();
}
Step4. 实现RequestPermissions接口(调用checkUpdateManger定义了该接口)。在该方法中调用DownloadService.startService开始下载最新版本的APP
@Override
public void call(Version version) {
this.mVersion = version;
requestExternalStorage();//检查app是否有读写磁盘权限
}
@AfterPermissionGranted(RC_EXTERNAL_STORAGE)
public void requestExternalStorage() {
if (EasyPermissions.hasPermissions(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
DownloadService.startService(this, mVersion.getDownloadUrl());
} else {
EasyPermissions.requestPermissions(this, "", RC_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE);
}
}
网友评论