我正在构建一个 Android 应用程序,用户可以在屏幕上绘制一些对象。一种类型的对象是文本对象。用户通过拖动手指来创建对象,并且该对象显示为可以移动/重塑形状的矩形。当用户点击文本对象时,我启动一个新活动,用户在其中输入文本,我在 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 );
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)