Android 开发收集的一些东西

Android - PickerView完善个人信息

2017-06-14  本文已影响743人  青见仔
GIF.gif

多级联动选择器的使用
地址:https://github.com/Bigkoo/Android-PickerView

依赖:compile 'com.contrarywind:Android-PickerView:3.2.5'

activity_more_info.xml布局代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.zjq.demo_zjq.activity.MoreInfoActivity">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:background="#4198b7"
        android:layout_width="match_parent"
        android:layout_height="50dp">

        <ImageView
            android:src="@mipmap/return_img"
            android:layout_width="25dp"
            android:layout_height="25dp" />

        <TextView
            android:id="@+id/tv_return"
            android:text="返回"
            android:textSize="16sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:text="完善个人信息"
            android:textSize="19sp"
            android:layout_centerInParent="true"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </android.support.v7.widget.Toolbar>


    <LinearLayout
        android:layout_marginTop="16dp"
        style="@style/common_horizontal_division_line_style"
        android:layout_width="match_parent"
        android:layout_height="1dp" />

    <RelativeLayout
        android:id="@+id/layout_photo"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/bg_layout_w2g"
        android:gravity="center_vertical|left"
        android:orientation="horizontal">

    <com.zjq.demo_zjq.ui.widget.CircleImageView
        android:id="@+id/cimg_photo"
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        app:civ_border_color="@color/grey"
        app:civ_border_width="1dp"
        app:srcCompat="@mipmap/ic_launcher_round" />
    </RelativeLayout>

    <LinearLayout
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        style="@style/common_horizontal_division_line_style"
        android:layout_width="match_parent"
        android:layout_height="1dp" />

    <LinearLayout
        android:id="@+id/layout_sex"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/bg_layout_w2g"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/activity_horizontal_margin"
            android:text="性别" />

        <TextView
            android:id="@+id/tv_sex"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/activity_horizontal_margin"
            android:layout_weight="1"
            android:ellipsize="end"
            android:maxLines="1" />
    </LinearLayout>

    <LinearLayout
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        style="@style/common_horizontal_division_line_style"
        android:layout_width="match_parent"
        android:layout_height="1dp" />

    <LinearLayout
        android:id="@+id/layout_age"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/bg_layout_w2g"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/activity_horizontal_margin"
            android:text="年龄" />

        <TextView
            android:id="@+id/tv_age"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/activity_horizontal_margin"
            android:layout_weight="1"
            android:ellipsize="end"
            android:maxLines="1" />
    </LinearLayout>

    <LinearLayout
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        style="@style/common_horizontal_division_line_style"
        android:layout_width="match_parent"
        android:layout_height="1dp" />

    <LinearLayout
        android:id="@+id/layout_address"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/bg_layout_w2g"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/activity_horizontal_margin"
            android:text="地址" />

        <TextView
            android:id="@+id/tv_address"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/activity_horizontal_margin"
            android:layout_weight="1"
            android:ellipsize="end"
            android:maxLines="1" />
    </LinearLayout>

    <LinearLayout
        style="@style/common_horizontal_division_line_style"
        android:layout_width="match_parent"
        android:layout_height="1dp" />

    <Button
        android:id="@+id/btn_submit"
        android:background="@drawable/shape_person"
        android:textColor="#ffffff"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_margin="@dimen/activity_horizontal_margin"
        android:text="@string/submit" />
</LinearLayout>
</ScrollView>

ProvinceBean:

   import com.bigkoo.pickerview.model.IPickerViewData;
   import java.util.List;

public class ProvinceBean  implements IPickerViewData {
/*
 * name : 北京市
 * city : [{"name":"北京市","area":["东城区","西城区","崇文区","宣武区","朝阳区","丰台区","石景山区","海淀区","门头沟区","房山区","通州区","顺义区","昌平区","大兴区","平谷区","怀柔区","密云县","延庆县"]}]
 */

private String name;
private List<CityBean> city;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public List<CityBean> getCity() {
    return city;
}

public void setCity(List<CityBean> city) {
    this.city = city;
}

@Override
public String getPickerViewText() {
    return this.name;
}

public static class CityBean {
    /**
     * name : 北京市
     * area : ["东城区","西城区","崇文区","宣武区","朝阳区","丰台区","石景山区","海淀区","门头沟区","房山区","通州区","顺义区","昌平区","大兴区","平谷区","怀柔区","密云县","延庆县"]
     */

    private String name;
    private List<String> area;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<String> getArea() {
        return area;
    }

    public void setArea(List<String> area) {
        this.area = area;
    }
}

}

Java代码 :

这里我用到butterknife注解

public class MoreInfoActivity extends Activity {

@BindView(R.id.cimg_photo)
CircleImageView cimgPhoto;
@BindView(R.id.layout_photo)
RelativeLayout layoutPhoto;
@BindView(R.id.tv_sex)
TextView tvSex;
@BindView(R.id.layout_sex)
LinearLayout layoutSex;
@BindView(R.id.tv_age)
TextView tvAge;
@BindView(R.id.layout_age)
LinearLayout layoutAge;
@BindView(R.id.tv_address)
TextView tvAddress;
@BindView(R.id.layout_address)
LinearLayout layoutAddress;
@BindView(R.id.btn_submit)
Button btnSubmit;
@BindView(R.id.tv_return)
TextView tvReturn;

private ArrayList<ProvinceBean> options1Items = new ArrayList<>();
private ArrayList<ArrayList<String>> options2Items = new ArrayList<>();
private ArrayList<ArrayList<ArrayList<String>>> options3Items = new ArrayList<>();

private ArrayList<String> ageItems = new ArrayList<>();
private ArrayList<String> sexItems = new ArrayList<>();

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_more_info);
    ButterKnife.bind(this);
}

@Override
protected void onResume() {
    super.onResume();
    options1Items = CityDataUtil.getProvinceData();
    options2Items = CityDataUtil.getCityData();
    options3Items = CityDataUtil.getAreData();
}

@OnClick({R.id.layout_photo, R.id.layout_sex, R.id.layout_age, R.id.layout_address, R.id.btn_submit ,R.id.tv_return})
public void onViewClicked(View view) {
    switch (view.getId()) {
        case R.id.layout_photo:
            //TODO 图片
            selectTOUpload();
            break;
        case R.id.layout_sex:
            //TODO 性别
            showSexPickView();
            break;
        case R.id.layout_age:
            //TODO 年龄
            showAgePickView();
            break;
        case R.id.layout_address:
            //TODO  地址选择器
            showCityPickView();
            break;
        case R.id.btn_submit:
            //TODO 提交
            updateUserInfo();
            break;
    }
}

//TODO 提交  v更新前需要先登录
private void updateUserInfo() {
    sex = tvSex.getText().toString();
    age = tvAge.getText().toString();
    address = tvAddress.getText().toString();
    if (!TextUtils.isEmpty(photoUrl)
            && !TextUtils.isEmpty(sex)
            && !TextUtils.isEmpty(age)
            && !TextUtils.isEmpty(address)) {
        Account newUser = new Account();
        newUser.setPhoto(photoUrl);
        newUser.setSex("男".equals(sex) ? true : false);
        newUser.setAge(Integer.valueOf(age));
        newUser.setAddress(address);
        Account bmobUser = BmobUser.getCurrentUser(MoreInfoActivity.this, Account.class);
        newUser.update(MoreInfoActivity.this, bmobUser.getObjectId(), new UpdateListener() {
            @Override
            public void onSuccess() {
                // TODO Auto-generated method stub
                ToastUtils.shortToast(MoreInfoActivity.this, getString(R.string.update_userinfo_success));
                PreferencesManager.getInstance(MoreInfoActivity.this).put(Constants.USER_PHOTO, photoUrl);
                MoreInfoActivity.this.finish();
            }

            @Override
            public void onFailure(int code, String msg) {
                // TODO Auto-generated method stub
                ToastUtils.shortToast(MoreInfoActivity.this, getString(R.string.update_userinfo_failed) + msg);
            }
        });
    } else {
        ToastUtils.shortToast(MoreInfoActivity.this, getString(R.string.checkinfo));
    }
}

//TODO 图片选择
private void selectTOUpload() {
    ImageConfig imageConfig
            = new ImageConfig.Builder(new GlideLoader())
            .steepToolBarColor(getResources().getColor(R.color.colorPrimary))
            .titleBgColor(getResources().getColor(R.color.colorPrimary))
            .titleSubmitTextColor(getResources().getColor(R.color.white))
            .titleTextColor(getResources().getColor(R.color.white))
            // (截图默认配置:关闭    比例 1:1    输出分辨率  500*500)
            .crop(1, 1, 300, 300)
            // 开启单选   (默认为多选)
            .singleSelect()
            // 开启拍照功能 (默认关闭)
            .showCamera()
            // 拍照后存放的图片路径(默认 /temp/picture) (会自动创建)
            .filePath("/six/Pictures")
            .build();
    ImageSelector.open(MoreInfoActivity.this, imageConfig);   // 开启图片选择器
}

//TODO 性别选择
private void showSexPickView() {
    sexItems.add("男");
    sexItems.add("女");
    OptionsPickerView pvOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
        @Override
        public void onOptionsSelect(int options1, int options2, int options3, View v) {
            tvSex.setText(sexItems.get(options1));
        }
    })
            .setTitleText(getString(R.string.select_sex))
            .setDividerColor(Color.BLACK)
            .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
            .setContentTextSize(20)
            .setOutSideCancelable(false)// default is true
            .build();
    pvOptions.setPicker(sexItems);//一级选择器
    pvOptions.show();
}


//TODO 年龄选择
private void showAgePickView() {
    for (int i = 1; i < 100; i++) {
        ageItems.add(String.valueOf(i));
    }
    OptionsPickerView pvOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
        @Override
        public void onOptionsSelect(int options1, int options2, int options3, View v) {
            tvAge.setText(ageItems.get(options1));
        }
    })
            .setTitleText(getString(R.string.select_age))
            .setDividerColor(Color.BLACK)
            .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
            .setContentTextSize(20)
            .setOutSideCancelable(false)// default is true
            .build();
    pvOptions.setPicker(ageItems);//一级选择器
    pvOptions.show();
}

//TODO  地址选择器
private void showCityPickView() {
    OptionsPickerView pvOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
        @Override
        public void onOptionsSelect(int options1, int options2, int options3, View v) {
            //返回的分别是三个级别的选中位置
            String tx = options1Items.get(options1).getPickerViewText() +
                    options2Items.get(options1).get(options2) +
                    options3Items.get(options1).get(options2).get(options3);
                    tvAddress.setText(tx);
        }
    })
            .setTitleText(getString(R.string.select_city))
            .setDividerColor(Color.BLACK)
            .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
            .setContentTextSize(20)
            .setOutSideCancelable(false)// default is true
            .build();
    /*pvOptions.setPicker(options1Items);//一级选择器
    pvOptions.setPicker(options1Items, options2Items);//二级选择器*/
    pvOptions.setPicker(options1Items, options2Items, options3Items);//三级选择器
    pvOptions.show();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == ImageSelector.IMAGE_REQUEST_CODE && resultCode == RESULT_OK && data != null) {
        // Get Image Path List
        List<String> pathList = data.getStringArrayListExtra(ImageSelectorActivity.EXTRA_RESULT);
        if (pathList.size() > 0) {
            //由于单选只需要回去第一个数据就好,获取图片URL并上传
            uploadPhotoForURL(pathList.get(0));
        } else {
            ToastUtils.shortToast(MoreInfoActivity.this, getString(R.string.select_pic_failed));
        }
    }
}

private void uploadPhotoForURL(String path) {
    final BmobFile bmobFile = new BmobFile(new File(path));
    bmobFile.uploadblock(MoreInfoActivity.this, new UploadFileListener() {

        @Override
        public void onSuccess() {
            //bmobFile.getFileUrl(context)--返回的上传文件的完整地址
            photoUrl = bmobFile.getFileUrl(MoreInfoActivity.this);
            ImageLoader.getInstance().displayImageTarget(cimgPhoto, photoUrl);
            ToastUtils.shortToast(MoreInfoActivity.this, getString(R.string.upload_photo_success) + photoUrl);
        }

        @Override
        public void onProgress(Integer value) {
            // 返回的上传进度(百分比)
        }

        @Override
        public void onFailure(int code, String msg) {
            ToastUtils.shortToast(MoreInfoActivity.this, getString(R.string.upload_failed) + msg);
        }
    });
}
}
上一篇下一篇

猜你喜欢

热点阅读