android - studio文本输入框 - 自定义edittext




使用customview获取输入文本而不需要edittext android (6)

尝试这个

InputMethodManager imm =    (InputMethodManager)     getSystemService(Context.INPUT_METHOD_SERVICE);
mSearchEditText.requestFocus();
imm.showSoftInput(mSearchEditText, InputMethodManager.SHOW_IMPLICIT);

我已经创建了customview。 每当用户在视图上双击时,它应该显示键盘,用户可以绘制新的文本。

Holder是一个扩展视图的customview。 但它显示键盘。 如何获得文本?

 public Holder(Context context, AttributeSet attrs) {
    super(context, attrs);
    Log.e(TAG,"EXE");
    imm = (InputMethodManager)
           context. getSystemService(Context.INPUT_METHOD_SERVICE);



 public boolean onDoubleTap(MotionEvent e) {        

        View view = Holder.this.getRootView();
        imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
     // imm.showSoftInput(Holder.this, InputMethodManager.SHOW_FORCED);  not working

检查这个链接 o试试这可能工作:

imm.showSoftInput(view,0);

您可以在您的活动中重写onKeyUp(int keyCode, KeyEvent event) ,然后将密钥信息( keyCode和/或event )发送到您的视图。 确保你有一个合适的自定义方法来处理视图中的键代码/事件(我们可以在这里调用它的handleKeyEvent )。

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    yourCustomView.handleKeyEvent(keyCode, event);
}

来源: https//groups.google.com/forum/#!topic/android-developers/0tQSZufLZTg


尝试动态创建edittext并将值发送到view.Here是我的code.its最好的选择..

public class CustomEditText extends EditText {

private KeyImeChange keyImeChangeListener;

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

public CustomEditText(Context context) {
    super(context);
}

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


public void setKeyImeChangeListener(KeyImeChange listener){
    keyImeChangeListener = listener;
}

public interface KeyImeChange {
    public void onKeyIme(int keyCode, KeyEvent event);
}

@Override
public boolean onKeyPreIme (int keyCode, KeyEvent event){
    if(keyImeChangeListener != null){
        keyImeChangeListener.onKeyIme(keyCode, event);
    }
    return false;
}

}

           MainActivity.editText2.setKeyImeChangeListener(new CustomEditText.KeyImeChange() {

                @Override
                public void onKeyIme(int keyCode, KeyEvent event) {
                //Update view on keyboard close
                      invalidate();


                }
            });

@Asthme:在检查如何捕获视图中的软键盘输入链接后 ,为了使一个视图可编辑您还需要使自定义BaseInputConnection使视图可编辑。

下面的链接提供了可能的代码解决方案,该链接来自Dianne Hackborne(Android Framework Engineer)的输入。

https://groups.google.com/forum/#!topic/android-developers/N0ITpZZ16Bw

直到最后的链接,你会发现自定义的BaseInputConnection使视图可编辑。

我正在发布示例代码的相关代码和屏幕截图:

class MyInputConnection extends BaseInputConnection {
    private SpannableStringBuilder myeditable;
    Holder mycustomview;

    public MyInputConnection(View targetView, boolean fullEditor) {
        super(targetView, fullEditor);
        mycustomview = (Holder) targetView;
    }

    public Editable getEditable() {
        if (myeditable == null) {
            myeditable = (SpannableStringBuilder) Editable.Factory.getInstance()
            .newEditable("Placeholder");
        }
        return myeditable;
    }

    public boolean commitText(CharSequence text, int newCursorPosition) {
        invalidate();
        myeditable.append(text);
        mycustomview.setText(text);
        return true;
    }
}

由于这是自定义视图,我们需要提供setText的实现。

    public void setText(CharSequence text) {
                mText = text;
                requestLayout();
                invalidate();
            }

下面是完整的自定义视图代码,这不同于你的实现(我离开手势* API未实现)

     package com.example.soappdemo;

        import android.content.Context;
        import android.graphics.Canvas;
        import android.graphics.Paint;
        import android.text.Editable;
        import android.text.InputType;
        import android.text.SpannableStringBuilder;
        import android.util.AttributeSet;
        import android.util.Log;
        import android.view.GestureDetector;
        import android.view.KeyEvent;
        import android.view.MotionEvent;
        import android.view.View;
        import android.view.inputmethod.BaseInputConnection;
        import android.view.inputmethod.EditorInfo;
        import android.view.inputmethod.InputConnection;
        import android.view.inputmethod.InputMethodManager;
        import android.widget.TextView;

        public class Holder extends View implements GestureDetector.OnDoubleTapListener {

            InputMethodManager imm;
            private Paint paint;
            private static final String TAG="Holder";
             private CharSequence mText="original";
            public Holder(Context context, AttributeSet attrs) {
                super(context, attrs);
                init();
                //requestFocus();
                setFocusableInTouchMode(true);
                setFocusable(true);
                requestFocus();


                setOnKeyListener(new OnKeyListener() {
                    public boolean onKey(View v, int keyCode, KeyEvent event) {
                        Log.d(TAG, "onKeyListener");
                        if (event.getAction() == KeyEvent.ACTION_DOWN) {
                            // Perform action on key press
                            Log.d(TAG, "ACTION_DOWN");
                            return true;
                        }
                        return false;
                    }
                });

            }

             public void init(){
                    paint = new Paint();
                    paint.setTextSize(12);
                    paint.setColor(0xFF668800);
                    paint.setStyle(Paint.Style.FILL);
                }

             @Override
                protected void onDraw(Canvas canvas) {
                    canvas.drawText(mText.toString(), 100, 100, paint);
                }

                @Override
                protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
                    this.setMeasuredDimension(150,200);     
                }

                public void setText(CharSequence text) {
                    mText = text;
                    requestLayout();
                    invalidate();
                }




            public boolean onTouchEvent(MotionEvent event) {
                super.onTouchEvent(event);
                Log.d(TAG, "onTOUCH");
                if (event.getAction() == MotionEvent.ACTION_UP) {

                    // show the keyboard so we can enter text
                    InputMethodManager imm = (InputMethodManager) getContext()
                            .getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.showSoftInput(this,0);
                }
                return true;
            }

            @Override
            public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
                Log.d(TAG, "onCreateInputConnection");

                outAttrs.actionLabel = null;
                outAttrs.label="TEST TEXT";
                outAttrs.inputType = InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS;
                outAttrs.imeOptions = EditorInfo.IME_ACTION_DONE;

                return new MyInputConnection(this,true);

            }

            @Override
            public boolean onCheckIsTextEditor() {
                Log.d(TAG, "onCheckIsTextEditor");
                return true;
            }

            @Override
            public boolean onDoubleTap(MotionEvent e) {
                // TODO Auto-generated method stub
                View view = Holder.this.getRootView();
                imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
                return false;
            }

            @Override
            public boolean onDoubleTapEvent(MotionEvent e) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean onSingleTapConfirmed(MotionEvent e) {
                // TODO Auto-generated method stub
                return false;
            }



            class MyInputConnection extends BaseInputConnection {
                private SpannableStringBuilder myeditable;
                Holder mycustomview;

                public MyInputConnection(View targetView, boolean fullEditor) {
                    super(targetView, fullEditor);
                    mycustomview = (Holder) targetView;
                }

                public Editable getEditable() {
                    if (myeditable == null) {
                        myeditable = (SpannableStringBuilder) Editable.Factory.getInstance()
                        .newEditable("Placeholder");
                    }
                    return myeditable;
                }

                public boolean commitText(CharSequence text, int newCursorPosition) {
                    invalidate();
                    myeditable.append(text);
                    mycustomview.setText(text);
                    return true;
                }
            }


        }

附加的是屏幕截图。 图片1是触摸之前,UI是一些文字。 Pic-2是当自定义视图被触摸,并从软键盘接收文本。 ![图1,自定义视图中的初始文本] [1]

![Pic-2 when custom view is tapped][2]





  [1]: http://i.stack.imgur.com/rLYmR.png
  [2]: http://i.stack.imgur.com/gTnYY.png

尝试这个....

view.requestFocus();
view.setFocusableInTouchMode(true);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(InputEditText, InputMethodManager.SHOW_FORCED);

也检查这一点

如何在edittext关注时显示软键盘





android-input-method