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)); } } } }
效果
相关文章推荐
- 底部弹出DialogFragment中使用ViewPager,ViewPager中使用Fragment出现Fragment does not have a view错误
- ViewPager+Fragment使用FragmentPageAdapter时app与v4下FragmentManager冲突的解决
- 如何通过代码控制软件键盘的显示与隐藏(InputMethodManager 的使用)
- 使用getChildFragmentManager解决Fragment嵌套ViewPager出现空白
- webview的input输入框获取焦点弹出数字键盘
- 移动端限制两位小数并且弹出数字键盘(解决使用H5S使用input[type='number']不兼容JS的问题)
- 使用InputMethodManager 退出键盘最灵活的方式
- Android ViewPaper使用FragmentPagerAdapter出现的FragmentManagerImpl为空的问题
- 解决 使用FragmentTabHost 和 viewpager时编译器报错 FragmentManager is already executing transactions
- 使用ViewPager和Fragment实现滑动导航
- viewpager+fragment时使用startactivity报错
- 使用ViewPager+Fragment来实现带滚动条的多屏滑动-IndicatorFragmentActivity
- 使用InputMethodManager时出现空指针异常
- 【移动开发】Android中Fragment+ViewPager的配合使用 推荐
- Android Fragment使用总结以及与Viewpager相结合使用
- 使用ViewPager+Fragment来实现带滚动条的多屏滑动-IndicatorFragmentActivity
- android webview 点击文本框弹出数字键盘
- ViewPager、Fragment、Matrix综合使用实现Tab滑页效果
- viewpager和Fragment的联合使用
- ViewPager+Fragment的结合使用,实现QQ界面的理解