我在Layout 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>
然后我实现了扩展View类来填充自定义布局:
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)”设置textView“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(...)”是不可能的! 求和:我想在“extends View”类中设置“EditText”的文本。我要指向外部布局...... mmmhh! 有办法吗?
答案 0 :(得分:1)
在自定义视图中有引用
public class Disegno extends View {
private TextView textView;
public void setTextView(TextView tv){
textview = tv;
}
@Override
protected void onDraw(Canvas canvas) {
textview.setText("Your text here!")
}
}
在您的Activity.onCreate()
中@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.my_layout);
EditText et_MOD_x = (EditText) findViewById(R.Id.et_MOD_x);
Disegno finestraDisegno = (Disegno)findViewById(R.Id.finestraDisegno);
finestraDisegno.setTextView(et_MOD_x) ;
}
答案 1 :(得分:0)
可能这不是正确的方法,但你可以:
Get your editText in your activity and declare it as static.
public static EditText et_MOD_x;
et_MOD_x = (EditText) findViewById(R.Id.et_MOD_x)
并在View类中按以下方式设置文本:
YourActivity.et_MOD_x.setText("abcd");
希望有所帮助......