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

  • 布局
<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="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));
            }
        }
    }
}
  • 效果
    这里写图片描述
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值