您的位置:首页 > 其它

InputMethodManager在ViewPager的Fragment中的使用(默认弹出数字键盘)

2018-01-30 14:29 519 查看
布局

<LinearLayout
android:layout_width="120dp"
android:layout_height="60dp"
android:layout_margin="@dimen/super_margin"
android:layout_gravity="center"
android:orientation="horizontal"
android:id="@+id/ll_input_long"
>

<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:gravity="center"
android:background="@drawable/backgrount_parking_number"
android:textColor="@color/material_text_main_write_100"
android:id="@+id/tv_input_first_long"
/>

<Space
android:layout_width="10dp"
android:layout_height="1dp"
/>

<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:gravity="center"
android:background="@drawable/backgrount_parking_number"
android:textColor="@color/material_text_main_write_100"
android:id="@+id/tv_input_second_long"
/>

<Space
android:layout_width="10dp"
android:layout_height="1dp"
/>

<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height=<
4000
span class="hljs-string">"match_parent"
android:gravity="center"
android:background="@drawable/backgrount_parking_number"
android:textColor="@color/material_text_main_write_100"
android:id="@+id/tv_input_third_long"
/>

<EditText
android:padding="@dimen/layout_padding"
android:background="@drawable/shape_primary_circle_around"
android:layout_gravity="center"
android:id="@+id/et_carport_no_long"
android:layout_width="1dp"
android:layout_height="1dp"
android:hint="@string/input_yellow_carport_no"
android:textColor="@color/material_text_main"
android:textColorHint="@color/material_text_hint"
android:descendantFocusability="beforeDescendants"
android:focusable="true"
android:focusableInTouchMode="true"
android:maxLines="1"
android:maxLength="3"
android:inputType="number"
android:singleLine="true" />

</LinearLayout>


代码

@SuppressLint("ValidFragment")
public class UserFragmeng extends Fragment implements View.OnClickListener{

private TextView tvCarportName;
private TextView tvCarportState;
private AppCompatImageView ivBattery;
private MyCarportListBean.DataBean data;
private Context mContext;
private int nowState;
private static final int DROP_STATE = 1;
private static final int UP_STATE = 2;
private AppCompatImageView avMore;
private LinearLayout llInput;
private TextView tvInputFirst;
private TextView tvInputSecond;
private TextView tvInputThird;
private ArrayList<TextView> inputList;
private EditText etInput;
private TextView ivDrop;
private ChargeRuleLayout crl;
private Animation alphaAnim;
private int action = -1;
private boolean etInputFlag = true;
private MyDialog loadingDialog;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.user_net_long_carport_new_fragmeng, container, false);

TextView tvParkName = (TextView) view.findViewById(R.id.tv_park_name);
TextView longLocation = (TextView) view.findViewById(R.id.tv_long_position);
TextView tvLongRentTime = (TextView) view.findViewById(R.id.tv_long_rent_time);
TextView tvLongGoPay = (TextView) view.findViewById(R.id.tv_long_go_pay);
tvLongGoPay.setOnClickListener(this);
tvParkName.setText(data.getName());
longLocation.setText(data.getParkAddress());
tvLongRentTime.setText(data.getEndTime());

alphaAnim = AnimManager.getClickDropAlpha(mContext);

etInput = (EditText) view.findViewById(R.id.et_carport_no_long);
ivDrop = (TextView) view.findViewById(R.id.tv_drop);
ivDrop.setOnClickListener(this);

//装到集合中
inputList = new ArrayList<>();
tvInputFirst = (TextView) view.findViewById(R.id.tv_input_first_long);
tvInputSecond = (TextView) view.findViewById(R.id.tv_input_second_long);
tvInputThird = (TextView) view.findViewById(R.id.tv_input_third_long);
inputList.add(tvInputFirst);
inputList.add(tvInputSecond);
inputList.add(tvInputThird);
crl = (ChargeRuleLayout) view.findViewById(R.id.crl);
//监听输入框的数据
etInput.addTextChangedListener(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) {

}

@Override
public void afterTextChanged(Editable s) {
//动态添加进输入框里面
String value = s.toString();
int length = value.length();
if(length>=3){
ivDrop.setSelected(true);
ivDrop.setClickable(true);
}else{
ivDrop.setSelected(false);
ivDrop.setClickable(false);
}
for(int i = 0 ; i < 3 ; i++){
if(i < length){
String show = value.substring(i,i+1);
inputList.get(i).setText(show);
inputList.get(i).setBackground(getResources().getDrawable(R.drawable.backgrount_parking_fill_number));
}else{
inputList.get(i).setText("");
inputList.get(i).setBackground(getResources().getDrawable(R.drawable.backgrount_parking_number));
}
}
if (etInput.getSelectionStart() >= 0 && etInput.getSelectionStart() < 3){
inputList.get(etInput.getSelectionStart()).setBackground(getResources().getDrawable(R.drawable.backgrount_parking_crude_number));
}
}
});

llInput = (LinearLayout) view.findViewById(R.id.ll_input_long);
llInput.setOnClickListener(this);

return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
mContext = context;
}

@Override
public void onClick(View v) {
int id = v.getId();

if(id==R.id.ll_input_long){
InputMethodManager imm = (InputMethodManager) etInput.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);

/**
* 非常重要-不设置这里的话,默认的会弹出中文或英文键盘
*/
etInput.requestFocus();
etInput.setFocusable(true);
etInput.setFocusableInTouchMode(true);
//第一次点击
if (etInputFlag){
etInputFlag = false;
inputList.get(0).setBackground(getResources().getDrawable(R.drawable.backgrount_parking_crude_number));
}
}
}
}


效果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: