0

我以编程方式创建了 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");
}

}

这是主要活动:

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

}
4

1 回答 1

3

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

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

:)

于 2013-06-12T19:50:21.917 回答