使用自定义视图获取输入文本,而无需编辑文本安卓



我创建了自定义视图。每当用户双击视图时,它应该显示键盘,用户可以绘制新文本。

持有人是扩展视图的自定义视图。但它显示了键盘。如何获取文本?

 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
这是一个

自定义FrameLayout,当您单击它时,它会显示soft-keyboard,您可以键入任何内容,当您按 Enter 时,它会显示一个包含您键入内容的 Toast 文本,我希望它能给你这个想法:

public class MyCustomFrameLayout extends FrameLayout {
    String mText;
    public MyCustomFrameLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }
    public MyCustomFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    public MyCustomFrameLayout(Context context) {
        super(context);
        init();
    }
    @Override
    public boolean onCheckIsTextEditor() {
        return true;
    }
    @Override
    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
            BaseInputConnection fic = new BaseInputConnection(this, false);
            outAttrs.actionLabel = null;
            outAttrs.inputType = InputType.TYPE_NULL;
            outAttrs.imeOptions = EditorInfo.IME_ACTION_DONE;
            return fic;      
    }
    public void init(){
        setFocusable(true);
        setFocusableInTouchMode(true);
        mText ="";
        setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    if((keyCode >= KeyEvent.KEYCODE_A) && (keyCode <= KeyEvent.KEYCODE_Z)) {
                        mText = mText + (char) event.getUnicodeChar();
                        return true;
                    }
                    else if(keyCode >= KeyEvent.KEYCODE_ENTER){                 
                        Toast.makeText(getContext(), "The text is: " + mText , Toast.LENGTH_LONG).show();
                         return true;
                    }
                }
                return false;
            }
        }); 
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            InputMethodManager imm = (InputMethodManager) getContext()
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(this, InputMethodManager.SHOW_FORCED);
        }
        return true;
    }
    public String getText(){        
        return mText;
    }
}

尝试动态创建编辑文本并将值发送到视图。这是我的代码,它的最佳选择..

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框架工程师)的输入。

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();
            }

下面是完整的自定义视图代码,这与您的实现不同(我未实现 Gesture* 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]: https://i.stack.imgur.com/rLYmR.png
  [2]: https://i.stack.imgur.com/gTnYY.png

检查此链接o试试这个它可能会起作用:

imm.showSoftInput(view,0);

试试这个

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

您可以覆盖活动中的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

试试这个....

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

还要检查这个

如何在编辑文本聚焦时显示软键盘

使用文本视图本身。文本视图上的可设置可编辑(true)使其行为类似于编辑文本。因此,在可设置可编辑(true)和可设置可设置(false)之间切换,以确定何时要编辑以及何时要读取

接受的答案对我不起作用。若要打开特定视图的键盘,它必须可聚焦,否则键盘不会显示。

android:focusable="true"
android:focusableInTouchMode="true"

这应该可以解决问题。

相关内容

  • 没有找到相关文章

最新更新