2013-06-12 102 views
0

我以编程方式创建了LinearLayout和EditText。我使用LinearLayout.draw(canvas)函数将LinearLayout绘制到画布上。 EditText可见但不可编辑。 我试过类似的东西:无法编辑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; 
} 

} 
+0

是否有什么原因需要将布局绘制到画布上? – dymmeh

+0

是的,在我们的应用程序中,一切都以编程方式创建。 – mgsujith

+1

您可以以编程方式创建所有内容,而无需手动绘制它们。 – dymmeh

回答

3

好像你只是画在画布上,而不是添加的EditText的布局。你只是在绘制,就像在ms paint中绘制html输入字段的图像一样。

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

:)

+0

我将EditText添加到LinearLayout,然后将LinearLayout绘制到画布上。我不知道如何将'linearLayout'添加到'mySurface'。 – mgsujith

+0

你应该使用你的Activity的setContentView方法 – ubergesundheit