我创建了自定义视图。每当用户双击视图时,它应显示键盘,用户可以绘制新文本。
Holder是一个扩展视图的自定义视图。但它正在显示键盘。如何获取文本?
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
答案 0 :(得分:6)
尝试动态创建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();
}
});
答案 1 :(得分:2)
@Asthme:检查链接后 How to Capture soft keyboard input in a View?,为了使View可编辑,您还需要自定义BaseInputConnection以使View可编辑。
可能的代码解决方案在下面的链接中给出,该链接来自Dianne Hackborne(Android Framework Engineer)。
https://groups.google.com/forum/#!topic/android-developers/N0ITpZZ16Bw
直到此链接的最后,您将找到自定义BaseInputConnection以使View可编辑。
我发布示例代码的相关代码和屏幕截图:
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;
}
}
}
附件是屏幕截图。 Pic-1在触摸之前,UI是一些文本。 Pic-2是触摸自定义视图并从软键盘接收文本时。 ![Pic-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
答案 2 :(得分:1)
检查link Ø 试试这可能有用:
imm.showSoftInput(view,0);
答案 3 :(得分:1)
试试这个
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mSearchEditText.requestFocus();
imm.showSoftInput(mSearchEditText, InputMethodManager.SHOW_IMPLICIT);
答案 4 :(得分:1)
您可以覆盖活动中的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
答案 5 :(得分:0)
试试这个......
view.requestFocus();
view.setFocusableInTouchMode(true);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(InputEditText, InputMethodManager.SHOW_FORCED);
也请查看
答案 6 :(得分:0)
使用textview本身。 textview上的Seteditable(true)使其行为类似于edittext。因此,在seteditable(true)和seteditable(false)之间进行切换,以了解何时编辑以及何时阅读
答案 7 :(得分:0)
接受的答案对我不起作用。要打开特定视图的键盘,它必须是可聚焦的,否则键盘将不会显示。
android:focusable="true"
android:focusableInTouchMode="true"
这应该可以解决问题。