2011-08-23 51 views
0

我已經插入一個佈局XML中稱爲「disegno」自定義視圖中樹立了「的TextView」的文本:它可以從一個類的擴展視圖「

<?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); } 
    ... 

現在我想設置TextView的「et_MOD_x」通過的文本「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(...)」是不可能的! 總結:我將從「extends View」類中設置「EditText」的文本。我想指向一個外部佈局...... mmmhh! 有什麼辦法可以做到這一點?

回答

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) ; 


} 
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) 

並在您的視圖類設置文本:

YourActivity.et_MOD_x.setText("abcd"); 

希望它有幫助..

相關問題