可以设置“TextView”的文本。从类中“扩展视图”
我在布局 xml 中插入了一个名为“disegno”的自定义视图:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?windowsBackground">
<it.package.myapp.Disegno
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/finestraDisegno"
android:isScrollContainer="true" />
<FrameLayout
android:layout_height="0dp"
android:layout_width="0dp"
...
<EditText
...
android:id="@+id/et_MOD_x">
</EditText>
然后我实现了扩展视图类来填充自定义布局:
package it.package.myapp;
import android.content.Context;
...
public class Disegno extends View {
public Disegno(Context context) {
this(context, null, 0); }
public Disegno(Context context, AttributeSet attrs) {
this(context, attrs, 0);
setContentView(R.layout.main); }
public Disegno(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
init(attrs); }
...
@Override
protected void onDraw(Canvas canvas) {
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
canvas.translate(mPosX, mPosY);
canvas.scale(mScaleFactor, mScaleFactor);
// chiamo routine assi cartesiani
AssiCartesiani(canvas);
// chiamo routine polilinea
Polilinea(canvas); }
...
现在我将通过“findViewById(R. Id.et_MOD_x)”像这样更改代码:
@Override
protected void onDraw(Canvas canvas) {
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
canvas.translate(mPosX, mPosY);
canvas.scale(mScaleFactor, mScaleFactor);
// chiamo routine assi cartesiani
AssiCartesiani(canvas);
// chiamo routine polilinea
Polilinea(canvas);
EditText et_MOD_x = (EditText) findViewById(R.Id.et_MOD_x)
et_MOD_x.setText("abcd");
}
但没有“setContentView(...)”是不可能的! 总结:我想从“扩展视图”类中设置“EditText”的文本。我想指出一个外部布局......嗯! 有什么办法可以做到这一点吗?!
I've insert a custom View called "disegno" inside a Layout xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?windowsBackground">
<it.package.myapp.Disegno
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/finestraDisegno"
android:isScrollContainer="true" />
<FrameLayout
android:layout_height="0dp"
android:layout_width="0dp"
...
<EditText
...
android:id="@+id/et_MOD_x">
</EditText>
Then I've implemented the extends View class for populate the custom layout:
package it.package.myapp;
import android.content.Context;
...
public class Disegno extends View {
public Disegno(Context context) {
this(context, null, 0); }
public Disegno(Context context, AttributeSet attrs) {
this(context, attrs, 0);
setContentView(R.layout.main); }
public Disegno(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
init(attrs); }
...
@Override
protected void onDraw(Canvas canvas) {
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
canvas.translate(mPosX, mPosY);
canvas.scale(mScaleFactor, mScaleFactor);
// chiamo routine assi cartesiani
AssiCartesiani(canvas);
// chiamo routine polilinea
Polilinea(canvas); }
...
Now I would to set the text of the textView "et_MOD_x" by "findViewById(R.Id.et_MOD_x)" changing the code like this:
@Override
protected void onDraw(Canvas canvas) {
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
canvas.translate(mPosX, mPosY);
canvas.scale(mScaleFactor, mScaleFactor);
// chiamo routine assi cartesiani
AssiCartesiani(canvas);
// chiamo routine polilinea
Polilinea(canvas);
EditText et_MOD_x = (EditText) findViewById(R.Id.et_MOD_x)
et_MOD_x.setText("abcd");
}
but without "setContentView(...)" is not possible!
Summing: I would to set the text of a "EditText" from within an "extends View" class. I'd to pointing to an external layout... mmmhh!
There is any way to do this?!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
自定义视图中有一个引用
在 Activity.onCreate() 的
have a reference in your custom view
in your Activity.onCreate()
可能这不是正确的方法,但您可以:
并在您的 View 类中设置文本:
希望它有帮助..
May be this is not the right way but you can :
and In your View class set the text by :
Hope it helps..