2010-09-10 69 views
0

如何按drawText點擊按鈕?如何在按鈕單擊時看到按鈕並繪製文本?我無法做到,下面是我繪製文本的代碼。 Android:Canvas.drawText按鈕點擊時

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    drawView = new DrawView(this); 
    setContentView(drawView); 
} 
public DrawView(Context context) { 
    super(context); 
    textpaint.setColor(Color.WHITE); 
} 
public void onDraw(Canvas canvas) { 
    canvas.drawText("Testing", 20, 55, textpaint); 
}

回答

0

DrawView繼承了什麼? 爲什麼不使用一個簡單的XML佈局在其中放置一個TextView和使用setVisibleOnClickListener

示例代碼中:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.my_view); 
    Button myButton = (Button) findViewById(R.id.my_button); 
    final MySurfaceView surfaceView = (MySurfaceView) findViewById(R.id.mysurface); 
    myButton.setOnClickListener(new OnClickListener() { 
     @Override 
     public boolean onClick(View v) { 
      // here you should change the position and the text you want to write 
      // like surfaceView.setCoordinates(x, y); 
      // and surfaceView.setTextToDraw("myText"); 
      return true; 
     } 
    }); 
} 

您需要創建自己的課程SurfaceView。如何做到這一點作爲一個例子:http://www.droidnova.com/playing-with-graphics-in-android-part-ii,160.html

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <org.mypackage.MySurfaceView android:id="@+id/mysurface" 
     android:layout_width="300dp" 
     android:layout_height="300dp" /> 
    <Button android:id="@+id/my_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Push it real good!" /> 
</LinearLayout> 
+0

文本的位置不能被編輯用這種方法,也只會出現1次。 – bamboolouie 2010-09-10 12:05:06

+0

請在你的問題中寫下下次...我爲你更新了我的答案 – WarrenFaith 2010-09-14 11:14:16