Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使编辑文本可点击?_Android_Android Canvas - Fatal编程技术网

Android 如何使编辑文本可点击?

Android 如何使编辑文本可点击?,android,android-canvas,Android,Android Canvas,很抱歉,如果这是一个重复的问题,我已经尝试了提供的解决方案和其他一些职位,但没有一个是为我工作 我已绘制编辑文本,但无法单击(触摸后未显示键盘) 我还从我看到的解决方案中尝试了这一点:(使用xml创建EditText) 但当我这么做时,应用程序总是强制关闭 主要问题是如何使EditText可点击并显示软键盘,就像您从xml中输入软键盘而不在画布上绘制一样您可以设置布局属性,如android:genderantfocusability=“beforesands”和android:focusable

很抱歉,如果这是一个重复的问题,我已经尝试了提供的解决方案和其他一些职位,但没有一个是为我工作

我已绘制编辑文本,但无法单击(触摸后未显示键盘)

我还从我看到的解决方案中尝试了这一点:(使用xml创建EditText)

但当我这么做时,应用程序总是强制关闭


主要问题是如何使EditText可点击并显示软键盘,就像您从xml中输入软键盘而不在画布上绘制一样

您可以设置布局属性,如
android:genderantfocusability=“beforesands”
android:focusableInTouchMode=“在此处输入代码”


愿这一条有用;)


默认情况下,如果单击编辑文本,键盘将可见,除非您做了一些更改行为。无需在xml布局中设置可单击、可聚焦。只需创建一个edittext并使用它。

如果您想使用edittext作为打开类似日期选择器对话框的内容,您可以使用
将焦点设置为flase。
因此单击edittext将打开对话框等


示例:
android:focusable=“flase”

不要在绘图方法中实例化视图!!!当视图或视图的一部分无效时,始终调用on draw。在自定义视图的构造函数中创建布局并添加它。您可能希望使用此editText2=(EditText)findViewById(R.id.editText2);firstEText.setOnFocusChangeListener(new View.OnFocusChangeListener(){@Override public void onFocusChange(视图v,布尔hasFocus){if(hasFocus){//todo:something}}});不要在draw方法中初始化视图,可以发布您的logcat堆栈跟踪吗?Fabian@CodeProcessor谢谢。编辑。我的logcat堆栈跟踪只包含返回的SurfaceView和锁定画布。@ASP我已经尝试过,并编写了System.out.println(“ABC”);替换//todo但在我触摸编辑文本后没有打印嗯,你能解释一下这是为了什么吗?我刚刚设置了它,没有任何更改。编辑文本不带焦点我已经输入了android:genderantfocusability=“beforesandroid”和android:focusableInTouchMode=“true”并尝试了“false”,但他们都没有做任何更改。我不知道为什么
public class GamePanel extends SurfaceView implements SurfaceHolder.Callback{
private LinearLayout lL;
private EditText editText;
@Override
public void surfaceCreated(SurfaceHolder holder){
    lL = new LinearLayout(getContext());

    editText = new EditText(getContext());

    editText.setFocusableInTouchMode(true);
    editText.setClickable(true);
    editText.requestFocus();

    editText.setText("Hello World");
    editText.setVisibility(View.VISIBLE);
    lL.addView(editText);
    lL.setDrawingCacheEnabled(true);
    lL.measure(canvas.getWidth(), canvas.getHeight());
    lL.layout(0, 0, canvas.getWidth(), canvas.getHeight());
    lL.bringChildToFront(editTextView);
}

@Override
public void draw(Canvas canvas){

    canvas.drawBitmap(lL.getDrawingCache(),50,50,null);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
</RelativeLayout>
EditText editText = (EditText)findViewById(R.id.editText1);
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
 android:descendantFocusability="beforeDescendants"
android:layout_height="match_parent">

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
</RelativeLayout>