Android Dialog中的EditText 弹出软键盘

其实,这个问题很简单,但是网上的好多资料,都走歪了。直接上代码:

final AlertDialog dialog = new AlertDialog.Builder(this).create();
    LayoutInflater layoutInflater = LayoutInflater.from(this);
    View view = layoutInflater.inflate(R.layout.modify_cart_num, null);
    dialog.setView(view);
    dialog.show();

    ImageView ivSub = (ImageView) view.findViewById(R.id.ivSub_cart_commodity);
    ImageView ivSum = (ImageView) view.findViewById(R.id.ivSum_cart_commodity);
    final EditText edCount = (EditText) view.findViewById(R.id.ed_count);
    edCount.setText(5+"");
    final Button btnCancel = (Button) view.findViewById(R.id.btn_cancel);
    final Button btnConfim = (Button) view.findViewById(R.id.btn_confirm);
    edCount.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //设置可获得焦点
            edCount.setFocusable(true);
            edCount.setFocusableInTouchMode(true);
            //请求获得焦点
            edCount.requestFocus();
            //调用系统输入法
            InputMethodManager inputManager = (InputMethodManager) edCount
                    .getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.showSoftInput(edCount, 0);
        }
    });

    btnCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });
    btnConfim.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            int count = parseInt(edCount.getText().toString());

            dialog.dismiss();
        }
    });

当然,最后是xml代码:

<!--<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"-->
          <!--android:layout_width="match_parent"-->
          <!--android:layout_height="wrap_content"-->
          <!--android:background="@android:color/white"-->
          <!--android:gravity="center_horizontal"-->
          <!--android:orientation="vertical">-->

<!--<TextView-->
    <!--android:layout_width="wrap_content"-->
    <!--android:layout_height="wrap_content"-->
    <!--android:layout_marginTop="15dp"-->
    <!--android:text="购买数量"-->
    <!--android:textColor="#333333"-->
    <!--android:textSize="16sp"/>-->


<!--<LinearLayout-->
    <!--android:id="@+id/llCount_cart_commodity"-->
    <!--android:layout_width="wrap_content"-->
    <!--android:layout_height="40dp"-->
    <!--android:layout_gravity="center"-->
    <!--android:layout_marginTop="20dp"-->

    <!--android:orientation="horizontal">-->

    <!--<ImageView-->
        <!--android:id="@+id/ivSub_cart_commodity"-->
        <!--android:layout_width="40dp"-->
        <!--android:layout_height="match_parent"-->
        <!--android:background="@android:color/white"-->
        <!--android:scaleType="fitCenter"-->
        <!--android:src="@drawable/ico_sub"/>-->


    <!--<LinearLayout-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="match_parent"-->
        <!--android:orientation="vertical">-->

        <!--<View-->
            <!--android:layout_width="match_parent"-->
            <!--android:layout_height="1dp"-->
            <!--android:background="@android:color/holo_green_light"></View>-->

        <!--<EditText-->
            <!--android:id="@+id/ed_count"-->
            <!--android:layout_width="70dp"-->
            <!--android:layout_height="0dp"-->
            <!--android:layout_weight="1"-->
            <!--android:layout_marginLeft="1dp"-->
            <!--android:layout_marginRight="1dp"-->
            <!--android:gravity="center"-->
            <!--android:text="1"-->
            <!--android:focusable="false"-->
            <!--android:background="@null"-->
            <!--android:textColor="@android:color/holo_green_light"-->
            <!--android:textSize="16sp"-->
            <!--/>-->

        <!--<View-->
            <!--android:layout_width="match_parent"-->
            <!--android:layout_height="1dp"-->
            <!--android:background="@android:color/holo_green_light"></View>-->
    <!--</LinearLayout>-->


    <!--<ImageView-->
        <!--android:id="@+id/ivSum_cart_commodity"-->
        <!--android:layout_width="40dp"-->
        <!--android:layout_height="match_parent"-->
        <!--android:background="@android:color/white"-->
        <!--android:scaleType="fitCenter"-->
        <!--android:src="@drawable/ico_sum"/>-->

<!--</LinearLayout>-->

<!--<View-->
    <!--android:layout_width="match_parent"-->
    <!--android:layout_height="1dp"-->
    <!--android:layout_marginTop="20dp"-->
    <!--android:background="@android:color/holo_green_light"/>-->

<!--<LinearLayout-->

    <!--android:layout_width="match_parent"-->
    <!--android:layout_height="45dp"-->
    <!--&gt;-->

    <!--<Button-->
        <!--android:id="@+id/btn_cancel"-->
        <!--android:layout_width="0dp"-->
        <!--android:layout_height="match_parent"-->
        <!--android:layout_weight="1"-->
        <!--android:gravity="center"-->
        <!--android:text="取消"-->
        <!--android:textColor="#333333"-->
        <!--android:textSize="15sp"/>-->


    <!--<Button-->
        <!--android:id="@+id/btn_confirm"-->
        <!--android:layout_width="0dp"-->
        <!--android:layout_height="match_parent"-->
        <!--android:layout_weight="1"-->
        <!--android:gravity="center"-->
        <!--android:text="确定"-->
        <!--android:textSize="15sp"/>-->
<!--</LinearLayout>-->

之后你就会发现,那光标问题怎么解决,怎么让光标处于始终最末端?其实自定义一个EditText就行了。代码如下:

public class CustomEditText extends EditText {
public CustomEditText(Context context) {
    super(context);
}

public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onSelectionChanged(int selStart, int selEnd) {
        super.onSelectionChanged(selStart, selEnd);
        //保证光标始终在最后面
        if(selStart==selEnd){//防止不能多选
            setSelection(getText().length());
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值