我正在构建一个 Android 应用程序,用户可以在其中绘制屏幕上的一些对象。一种类型的对象是文本对象。用户通过拖动他的手指来创建对象,对象显示为可以移动/ reshape 形状的矩形。当用户点击文本对象时,我会启动一个新 Activity ,用户在该 Activity 中输入我在 onActivityResult 方法中返回的文本。
现在我想显示对象中的文本。我可以从我的文本类中访问诸如矩形等坐标的东西。从本质上讲,我想做的是以编程方式创建一个 TextView(或 EditText),并将其边界设置为绘制对象的矩形边界。有没有一种方法可以帮助我做到这一点?
(另一种方法是在我的文本对象中使用 canvas.drawTextOnPath 方法。但这似乎更复杂,因为我的文本可能会脱离对象,而且我还必须处理多行)
非常感谢您!
编辑:尝试 GAMA 的方法
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
case 1:
if (resultCode == Activity.RESULT_OK) {
String text=data.getStringExtra("text");
System.out.println(text);
TextView tv=new TextView(this);
//LayoutParams lp = new LayoutParams(new ViewGroup.MarginLayoutParams((int)texts.get(index).width,(int)texts.get(index).height));
LayoutParams lp = new LayoutParams(new ViewGroup.MarginLayoutParams(100,100));
//tv.setLayoutParams(lp);
//lp.setMargins((int)texts.get(index).Sx, (int)texts.get(index).Sy, (int)texts.get(index).Lx, (int)texts.get(index).Ly);
tv.setLayoutParams(lp);
tv.setTextSize(10);
tv.setTextColor(Color.RED);
tv.setText(text);
lp.setMargins(0,0,0,0);
//tv.setVisibility(View.VISIBLE);
System.out.println("got "+tv.getText());
}
break;
}
}
两个打印件都按预期显示文本,但我在屏幕上看不到任何内容(尝试将其设置在左下角开始)
最佳答案
EditText edText = new EditText(this);
edText .setId(i);
edText .setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
1f));
edText .setWidth(100);
edText .setImeOptions(EditorInfo.IME_ACTION_NEXT);
edText .setInputType(InputType.TYPE_CLASS_NUMBER);
edText .setKeyListener(DigitsKeyListener.getInstance());
edText .setMaxLines(1);
edText .setOnFocusChangeListener(this);
edText .setOnEditorActionListener(this);
edText .addTextChangedListener(this);
//this linearlayout id is declared inside your xml file
LinearLayout linear=(LinearLayout)findViewById(R.id.linearLayout1);
linear.addView(edText );
关于Android - 以编程方式创建 TextView(或 EditText)并将其设置在屏幕上提供坐标的特定位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10894733/