大家好,我是飞刀,这次我来讲一下布局XML转findView的功能。
首先打开界面看一下:

界面上有一个view名称的输入框和主输入框。
找来一段控件带id属性的复杂xml布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="@layout/include_goods_details" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="57dp"
android:gravity="center_vertical"
android:background="@mipmap/auction_price_back"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/tv_nowPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="¥76.00"
android:textColor="@color/c2"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="一口价 :上不封顶"
android:textColor="@color/c2"
android:textSize="12sp" />
</LinearLayout>
<FrameLayout
android:layout_width="158dp"
android:layout_height="match_parent"
>
<LinearLayout
android:id="@+id/ll_not_start"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<include layout="@layout/include_from_the_end_of_time"/>
</LinearLayout>
<TextView
android:id="@+id/tv_underway"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="@color/c13"
android:textSize="26sp"
android:visibility="gone"
android:text="进行中" />
<TextView
android:id="@+id/tv_finished"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:visibility="gone"
android:textColor="@color/c13"
android:textSize="26sp"
android:text="已结束" />
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/c2"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/tv_activityName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全消光-DTY-134dt/72f全消光-DT-DT"
android:textColor="@color/c1"
android:textSize="16sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/iv_head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/auction_cash_deposit"/>
<TextView
android:id="@+id/tv_cashDeposit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保证金 :¥10000.00"
android:layout_marginLeft="5dp"
android:textColor="@color/c6"
android:textSize="13sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:orientation="horizontal">
<TextView
android:layout_width="40dp"
android:layout_height="wrap_content"
android:text="加价"
android:textColor="@color/c6"
android:textSize="14sp" />
<EditText
android:id="@+id/et_addPrice"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:background="@drawable/rectangle_solid_c4"
android:gravity="center"
android:inputType="number"
android:textColor="@color/c1"
android:textSize="14sp" />
<TextView
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="元/吨"
android:textColor="@color/c1"
android:textSize="14sp" />
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<TextView
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="需求量"
android:textColor="@color/c6"
android:textSize="14sp" />
<EditText
android:id="@+id/et_amounts"
android:layout_width="60dp"
android:layout_height="match_parent"
android:background="@drawable/rectangle_solid_c4"
android:gravity="center"
android:inputType="number"
android:textColor="@color/c1"
android:textSize="14sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="吨"
android:textColor="@color/c1"
android:textSize="14sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_mininumAddPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="加价幅度:¥100.00"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:background="@drawable/rectangle_solid_c10_corner_13dp"
android:textColor="@color/c13"
android:textSize="12sp" />
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<TextView
android:id="@+id/tv_amounts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="最少需求量:10吨"
android:textColor="@color/c13"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:background="@drawable/rectangle_solid_c10_corner_13dp"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@color/c2"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:layout_marginBottom="10dp"
android:text="出价记录"
android:textColor="@color/c1"
android:drawablePadding="5dp"
android:drawableLeft="@drawable/rectangle_solid_c13_width_3dp_height_15dp"
android:textSize="16sp" />
<View
android:layout_width="match_parent"
android:layout_height="@dimen/line"
android:background="@color/line_color" />
<com.zbase.view.adapterview.LinearListView
android:id="@+id/linearListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:lineDrawable="@color/line_color" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="67dp">
<TextView
android:id="@+id/tv_see_all_bid"
android:layout_width="96dp"
android:layout_height="26dp"
android:layout_gravity="center"
android:textColor="@color/c13"
android:gravity="center"
android:textSize="12sp"
android:background="@drawable/rectangle_solid_c2_stroke_c6_corner_23dp"
android:text="查看全部出价" />
</FrameLayout>
<include layout="@layout/include_product_parameters"/>
<include layout="@layout/include_enter_the_store"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="93dp"
/>
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="@dimen/line"
android:background="@color/line_color" />
<FrameLayout
android:layout_width="match_parent"
android:background="@color/c2"
android:layout_height="82dp">
<TextView
android:id="@+id/tv_btn"
android:layout_width="117dp"
android:layout_height="35dp"
android:textColor="@color/c2"
android:layout_marginRight="28dp"
android:layout_gravity="right|center_vertical"
android:gravity="center"
android:textSize="18sp"
android:background="@drawable/rectangle_solid_c13_corner_20dp"
android:text="我要出价" />
</FrameLayout>
</LinearLayout>
</FrameLayout>
点击生成,看一下输出框里面生成的结果:
private TextView tv_nowPrice;
private LinearLayout ll_not_start;
private TextView tv_underway;
private TextView tv_finished;
private TextView tv_activityName;
private ImageView iv_head;
private TextView tv_cashDeposit;
private EditText et_addPrice;
private EditText et_amounts;
private TextView tv_mininumAddPrice;
private TextView tv_amounts;
private LinearListView linearListView;
private TextView tv_see_all_bid;
private TextView tv_btn;
tv_nowPrice = view.findViewById(R.id.tv_nowPrice);
ll_not_start = view.findViewById(R.id.ll_not_start);
tv_underway = view.findViewById(R.id.tv_underway);
tv_finished = view.findViewById(R.id.tv_finished);
tv_activityName = view.findViewById(R.id.tv_activityName);
iv_head = view.findViewById(R.id.iv_head);
tv_cashDeposit = view.findViewById(R.id.tv_cashDeposit);
et_addPrice = view.findViewById(R.id.et_addPrice);
et_amounts = view.findViewById(R.id.et_amounts);
tv_mininumAddPrice = view.findViewById(R.id.tv_mininumAddPrice);
tv_amounts = view.findViewById(R.id.tv_amounts);
linearListView = view.findViewById(R.id.linearListView);
tv_see_all_bid = view.findViewById(R.id.tv_see_all_bid);
tv_btn = view.findViewById(R.id.tv_btn);
tv_nowPrice.setOnClickListener(this);
ll_not_start.setOnClickListener(this);
tv_underway.setOnClickListener(this);
tv_finished.setOnClickListener(this);
tv_activityName.setOnClickListener(this);
iv_head.setOnClickListener(this);
tv_cashDeposit.setOnClickListener(this);
tv_mininumAddPrice.setOnClickListener(this);
tv_amounts.setOnClickListener(this);
linearListView.setOnClickListener(this);
tv_see_all_bid.setOnClickListener(this);
tv_btn.setOnClickListener(this);
tv_nowPrice.setText(bean.getNowPrice() + "");
tv_underway.setText(bean.getUnderway() + "");
tv_finished.setText(bean.getFinished() + "");
tv_activityName.setText(bean.getActivityName() + "");
Glide.with(context).load(bean.getHead()).into(iv_head);
tv_cashDeposit.setText(bean.getCashDeposit() + "");
tv_mininumAddPrice.setText(bean.getMininumAddPrice() + "");
tv_amounts.setText(bean.getAmounts() + "");
tv_see_all_bid.setText(bean.getSeeAllBid() + "");
tv_btn.setText(bean.getBtn() + "");
switch (v.getId()) {
case R.id.tv_nowPrice:
break;
case R.id.ll_not_start:
break;
case R.id.tv_underway:
break;
case R.id.tv_finished:
break;
case R.id.tv_activityName:
break;
case R.id.iv_head:
break;
case R.id.tv_cashDeposit:
break;
case R.id.tv_mininumAddPrice:
break;
case R.id.tv_amounts:
break;
case R.id.linearListView:
break;
case R.id.tv_see_all_bid:
break;
case R.id.tv_btn:
break;
}
LinkedHashMap<TextView, String> map = new LinkedHashMap<>();
map.put(et_addPrice, "");
map.put(et_amounts, "");
if (!ViewUtil.verifyTextViewMapEmpty(map)) {
return;
}
首先是生成了各种控件的声明,如TextView,LinearLayout,ImageView,EditText,其他的所有控件都支持,如Button,自定义控件等。
然后是 view.findViewById语句,上面那个默认的view输入框就是定义这里view的名称,可根据自己的项目填写。然后是控件的setOnClickListener(this)事件注册,这里只要复制自己需要的部分语句。同理TextView的setText()语句按需复制过来,删除无用的代码。还有Glide.with(context).load(bean.getHead()).into(iv_head);这种图片加载的代码,我是用的Glide。
然后是事件的switch case语句也是按需复制。还有一个ViewUtil.verifyTextViewMapEmpty()的方法,这个是用来干什么的呢?我把方法代码贴出来你应该就知道了:
/**
* 验证多个EditText或TextView的内容不能为空(EditText继承TextView)
* @param editTextMap
* Map的有序和无序实现类,与Map的排序
* 1.HashMap、Hashtable是无序的;
* 2.TreeMap和LinkedHashMap是有序的(TreeMap默认Key升序,LinkedHashMap则记录了插入顺序,所以这里用TreeMap会报错,使TextView不能用于排序)。
* @return
*/
public static boolean verifyTextViewMapEmpty(LinkedHashMap<TextView,String> editTextMap){
for(Map.Entry<TextView, String> entry : editTextMap.entrySet()){
TextView editText = entry.getKey();
String editTextName = entry.getValue();
String text=editText.getText().toString();
if (TextUtils.isEmpty(text)) {
PopUtil.toast(editTextName+"不能为空");
return false;
}
}
return true;
}
最近还新增生成了okhttp请求的参数构造,如下:
request.params("productName", et_productName.getText().toString());
request.params("specifications", et_specifications.getText().toString());
实际使用的代码:
/**
* 请求添加收货地址
*/
private void requestMemberAddressAdd() {
LinkedHashMap<TextView, String> map = new LinkedHashMap<>();
map.put(tv_company_name, "公司名称");
map.put(et_username, "收货人姓名");
map.put(vet_phone, "收货人电话");
map.put(et_post_code, "邮政编码");
map.put(tv_address, "收货地址");
map.put(et_detail_address, "详细地址");
if (ViewUtil.verifyTextViewMapEmpty(map) && vet_phone.verify()) {
PostRequest request = OkGo.<CommonBean>post(HttpConstant.getCacheUrl() + HttpConstant.MEMBER_ADDRESS_ADD).tag(this);
request.params("memberId", ProjectUtil.getLoginData().getMemberId());
request.params("name", et_username.getText().toString());
request.params("companyName", tv_company_name.getText().toString());
request.params("name", et_username.getText().toString());
request.params("phoneNumber", vet_phone.getText().toString());
request.params("postCode", et_post_code.getText().toString());
request.params("province", mProvinceName);
request.params("city", mCityName);
request.params("region", mCountyName);
request.params("detailAddress", et_detail_address.getText().toString());
request.execute(new SignJsonCallback<CommonBean>(context, CommonBean.class, false) {
@Override
public void onSuccess(CommonBean bean) {
if (bean.getCode() == 200) {
PopUtil.toast("添加收货地址成功");
finish();
} else {
PopUtil.toast(bean.getMessage());
}
}
});
}
}
这个方法会依次判断公司名称,收货地址,收货人电话等是否为空,如果为空则提示,并return。是不是比较方便。
还有,这个功能不仅支持整个xml布局的代码转换,也支持部分代码的转换哦。
复制一段部分xml代码:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="20dp"
android:text="手机号码"
android:textColor="@color/c1"
android:textSize="16sp" />
<com.zbase.view.edittext.VerifyEditText
android:id="@+id/et_contactPhone"
android:layout_width="310dp"
android:layout_height="55dp"
android:layout_gravity="right|center_vertical"
android:background="@null"
android:hint="请填写企收货人·手机号·"
android:maxLength="11"
android:paddingLeft="15dp"
app:inputEnum="phone"
android:textSize="15sp" />
点击生成如下代码:
private VerifyEditText et_contactPhone;
et_contactPhone = view.findViewById(R.id.et_contactPhone);
et_contactPhone.setOnClickListener(this);
switch (v.getId()) {
case R.id.et_contactPhone:
break;
}
看,这样也可以哦,只要保证控件代码<括号的完整性就可以了,剩下的自己探索吧。
最后说一下为什么不使用ButterKnife。相信很多小伙伴有使用或见过这个findViewById的框架。
首先是项目通用性,使用原始的findViewById不会产生框架兼容的问题,不管是新旧项目都可以使用。而引入框架则会导致有些项目有使用,有些项目没有使用的情况。反而会增加项目的复杂度,降低项目的可读性等。其实是版本差异问题,我记得ButterKnife框架有些版本写法不一样,导致新旧项目需要升级等问题。
第三个就是增加了学习成本和沟通成本,要去记很多的@Bind注解,对新人也不友好。
而使用原始的findViewById不会产生这些问题,不管是自定义控件,还是第三方控件一般也是原始的findViewById,所以可读性和兼容性更好。最后到底要不要使用那就是仁者见仁智者见智了。
所以我在这里推荐用我的这个代码生成器去生成。这样既解决了手写findViewById的效率问题,也不会产生ButterKnife的副作用,而且还增加了ButterKnife不具备的功能,何乐而不为呢?
好了,今天的内容分享就到这了,我们下篇见。
【我是小尾巴】自创的安卓快速开发框架,自创的自定义控件,自创的代码生成器,自创的代码模板,自创的demo集合。并开始涉猎springboot,vue,uniapp,unicloud,云函数,云数据库,cocoscreator游戏开发。同时也分享一些好用的软件,开发工具,学习视频。关注微信公众号【飞刀帮主】免费获取资料。

913

被折叠的 条评论
为什么被折叠?



