无法编辑EditText

时间:2013-06-12 19:28:42

标签: android android-layout

我已经以编程方式创建了LinearLayout和EditText。我使用LinearLayout.draw(canvas)函数将LinearLayout绘制到画布上。 EditText可见但不可编辑。 我尝试过这样的事情:

editText.setText("Hi this is test:", BufferType.EDITABLE);
editText.setEnabled(true);
editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
editText.setClickable(true);
没什么作用。我错过了什么?

以下是代码:

public class mySurface extends SurfaceView implements Runnable {

SurfaceHolder holder;
EditText editText = null;
TextView tv = null;
LinearLayout linearLayout;
Context context;
Thread thread = null;

public mySurface(Context context) {
    super(context);
    this.context = context;
    holder = getHolder();
    editText = new EditText(context);
    tv = new TextView(context);
    linearLayout = new LinearLayout(context);
}

@Override
public void run() {
    Canvas canvas;
    while (true) {
        if (holder.getSurface().isValid()) {
            canvas = holder.lockCanvas();
            canvas.drawColor(Color.WHITE);
            canvas.save();
            editText.setMaxWidth(1000);
            editText.setMaxHeight(50);
            editText.setTextSize(20f);
            editText.setText("Hi this is test:", BufferType.EDITABLE);
            editText.setEnabled(true);
            editText.setFocusable(true);
            editText.setFocusableInTouchMode(true);
            editText.setClickable(true);

            linearLayout.measure(100, 100);
            linearLayout.layout(0, 0, 100, 100);
            if (editText.getParent() == null){
                linearLayout.addView(editText);
            }
            canvas.save();
            canvas.translate(100, 100);
            linearLayout.draw(canvas);
            canvas.restore();
            holder.unlockCanvasAndPost(canvas);
        }
    }
}

public void onResumeMySurfaceView() {
    thread = new Thread(this);
    thread.start();
    // Log.v("Resumed", "Running");
}

}

以下是MainActivity:

public class MainActivity extends Activity {

mySurface view;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    view = new mySurface(this);
    setContentView(view);
}

@Override
protected void onResume() {
    super.onResume();
    view.onResumeMySurfaceView();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

1 个答案:

答案 0 :(得分:3)

似乎您只是在画布上绘图而不是将EditText添加到布局中。你只是画画,就像你在ms画中画一个html输入字段的图像一样。

你应该考虑阅读这个。 http://developer.android.com/reference/android/app/Activity.html#setContentView%28android.view.View%29

:)