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代码:


          
          
          
          
          


    
    
    
    
    
    



    
    
    
    
    

    

    
        
        
        
        
        
        


    
        
        
        

        
            
            
            

        
            
            
            
            
            
            
            
            
            
            
            
            
            

        
            
            
            
    


    
        
        
        
        
        
        




    
    
    
    



    
    
    

    
        
        
        
        
        
        
        
        


    
        
        
        
        
        
        
        

之后你就会发现,那光标问题怎么解决,怎么让光标处于始终最末端?其实自定义一个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());
        }

    }
}

你可能感兴趣的:(Android)