RecyclerView 多條目型別含有多個EditText
public class PositionSalaryModifyAdapter extends BaseMultiItemQuickAdapter<SalaryBean.RowsBean, BaseViewHolder> {
/**
* Same as QuickAdapter#QuickAdapter(Context,int) but with
* some initialization data.
*
* @param data A new list is created out of this one to avoid mutable list
*/
public PositionSalaryModifyAdapter(List<SalaryBean.RowsBean> data) {
super(data);
addItemType(1, R.layout.item_position_salary_modify_type1);
addItemType(2, R.layout.item_position_salary_modify_type2);
}
@Override
protected void convert(@NonNull BaseViewHolder holder, SalaryBean.RowsBean bean) {
switch (holder.getItemViewType()) {
case 1:
holder.setText(R.id.tv_department_name, bean.getDepName());
holder.setText(R.id.edt_zjl_salary, setMoney(bean.getGeneral()));
holder.setText(R.id.edt_fzjl_salary, setMoney(bean.getVice()));
int position = holder.getLayoutPosition();
EditText edtZjlSalary = holder.getView(R.id.edt_zjl_salary);
EditText edtFzjlSalary = holder.getView(R.id.edt_fzjl_salary);
editSetOnFocusChangeListener(edtZjlSalary, position, 0);
editSetOnFocusChangeListener(edtFzjlSalary, position, 1);
break;
case 2:
holder.setText(R.id.tv_department_name, bean.getDepName());
holder.setText(R.id.edt_bmjl_salary, setMoney(bean.getDepartment()));
holder.setText(R.id.edt_cbjl_salary, setMoney(bean.getTrainee()));
holder.setText(R.id.edt_bmzg_salary, setMoney(bean.getCharge()));
holder.setText(R.id.edt_cbzg_salary, setMoney(bean.getReserve()));
holder.setText(R.id.edt_yg_salary, setMoney(bean.getStaff()));
int positionTwo = holder.getLayoutPosition();
EditText edtBmjlSalary = holder.getView(R.id.edt_bmjl_salary);
EditText edtCbjlSalary = holder.getView(R.id.edt_cbjl_salary);
EditText edtBmzgSalary = holder.getView(R.id.edt_bmzg_salary);
EditText edtCbzgSalary = holder.getView(R.id.edt_cbzg_salary);
EditText edtYgSalary = holder.getView(R.id.edt_yg_salary);
editSetOnFocusChangeListener(edtBmjlSalary, positionTwo, 2);
editSetOnFocusChangeListener(edtCbjlSalary, positionTwo, 3);
editSetOnFocusChangeListener(edtBmzgSalary, positionTwo, 4);
editSetOnFocusChangeListener(edtCbzgSalary, positionTwo, 5);
editSetOnFocusChangeListener(edtYgSalary, positionTwo, 6);
break;
}
}
private String setMoney(double money) {
DecimalFormat df = new DecimalFormat("######0.00");
if (money == 0.0 || money == 0.00 || money == 0) {
return "";
}
return df.format(money);
}
//以下程式碼為重點
private void editSetOnFocusChangeListener(EditText edt, int position, int i) {
TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//刪除.後面超過兩位的數字
if (s.toString().contains(".")) {
if (s.length() - 1 - s.toString().indexOf(".") > 2) {
s = s.toString().subSequence(0,
s.toString().indexOf(".") + 3);
edt.setText(s);
edt.setSelection(s.length());
}
}
//如果.在起始位置,則起始位置自動補0
if (s.toString().trim().substring(0).equals(".")) {
s = "0" + s;
edt.setText(s);
edt.setSelection(2);
}
//如果起始位置為0並且第二位跟的不是".",則無法後續輸入
if (s.toString().startsWith("0")
&& s.toString().trim().length() > 1) {
if (!s.toString().substring(1, 2).equals(".")) {
edt.setText(s.subSequence(0, 1));
edt.setSelection(1);
return;
}
}
}
@Override
public void afterTextChanged(Editable s) {
switch (i) {
case 0:
if (s.length() > 0) {
getData().get(position).setGeneral(Double.parseDouble(s.toString()));
} else {
getData().get(position).setGeneral(0.00);
}
break;
case 1:
if (s.length() > 0) {
getData().get(position).setVice(Double.parseDouble(s.toString()));
} else {
getData().get(position).setVice(0.00);
}
break;
case 2:
if (s.length() > 0) {
getData().get(position).setDepartment(Double.parseDouble(s.toString()));
} else {
getData().get(position).setDepartment(0.00);
}
break;
case 3:
if (s.length() > 0) {
getData().get(position).setTrainee(Double.parseDouble(s.toString()));
} else {
getData().get(position).setTrainee(0.00);
}
break;
case 4:
if (s.length() > 0) {
getData().get(position).setCharge(Double.parseDouble(s.toString()));
} else {
getData().get(position).setCharge(0.00);
}
break;
case 5:
if (s.length() > 0) {
getData().get(position).setReserve(Double.parseDouble(s.toString()));
} else {
getData().get(position).setReserve(0.00);
}
break;
case 6:
if (s.length() > 0) {
getData().get(position).setStaff(Double.parseDouble(s.toString()));
} else {
getData().get(position).setStaff(0.00);
}
break;
}
}
};
edt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
edt.addTextChangedListener(textWatcher);
} else {
edt.removeTextChangedListener(textWatcher);
}
}
});
}
}
相關文章
- Android RecyclerView多型別佈局卡片解決方案AndroidView多型型別
- MultiItem用法與詳解-優雅的實現多型別RecyclerView Adapter多型型別ViewAPT
- Android多個EditText輸入效果解決方案Android
- 實現多型多對多 幾個注意點多型
- EditText輸入金額型別型別
- MultiItem進階 實現Head Foot和載入更多-多型別RecyclerView Adapter多型型別ViewAPT
- C#中含有多個Main函式的處理C#AI函式
- 一個RecyclerView實現多級摺疊列表(二)View
- 一個RecyclerView實現多級摺疊列表(TreeRecyclerView)View
- Java多型實現的必要條件Java多型
- 用record型別和table型別儲存查詢語句返回的多條記錄 的例子型別
- 多型中的返回值型別多型型別
- swift 存放多型別的容器Swift多型型別
- 建立多種型別的流型別
- RecyclerView使用,優化,條目閃爍問題View優化
- Python:多型、協議和鴨子型別Python多型協議型別
- oracle一個別例項配置多個listener或多個埠Oracle
- 【Go】那麼多數值型別,應該選哪個?Go型別
- 拆分含有多種分隔符的字串字串
- IM多型別holder封裝多型型別封裝
- 多條目載入(附帶透明的屬性動畫)動畫
- 多型。多型
- 多型~多型
- 多型多型
- RecyclerView使用指南(二)—— 多種ItemLayoutView
- 程式碼優化-多型代替IF條件判斷優化多型
- IOS多型別Cell的tableView實現iOS多型型別View
- C++和java多型的區別C++Java多型
- expdp測試包含有lob型別的物件型別物件
- 如何批量複製多個檔案到多個目錄中(批量複製檔案,多對多檔案高效操作的方法)
- 條件型別型別
- Java多型Java多型
- 多型一多型
- 11 #### 多型多型
- 多型性多型
- 重寫、覆蓋、過載、多型幾個概念的區別分析多型
- 淺析多媒體互動展廳幾個常見型別型別
- jquery 多條件匹配jQuery