1
我正在試圖製作一個簡單的應用程序,它可以顯示矩形並可以通過按鈕更改其顏色。通過按鈕更改矩形的顏色
Rectangle類是:
public class DrawView extends View{
Paint paint = new Paint();
public DrawView(Context context) {
super(context);
}
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DrawView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onDraw(Canvas canvas) {
paint.setColor(Color.YELLOW);
canvas.drawRect(300, 550, 150, 400, paint);
}
public void setColorRed()
{
paint.setColor(Color.RED);
invalidate();
}
我的應用程序選項卡布局應用程序。
main.xml中
<TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/bRedColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red" />
<com.thms.systemy3.DrawView
android:id="@+id/yourID"
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</com.thms.systemy3.DrawView>
</FrameLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
我試着
DrawView drawview;
以訪問從我MainClass.java類setColorRed():這個類是按這種方式顯示在第三個標籤
然後用
drawview.setColorRed()
MainClass.java:
public class MainClass extends Activity implements OnClickListener{
TabHost th;
DrawView drawview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
th = (TabHost) findViewById(R.id.tabhost);
//tab3
Button bColorRed = (Button) findViewById(R.id.bRedColor);
th.setup();
TabSpec specs = th.newTabSpec("tag1");
specs.setContent(R.id.tab1);
specs.setIndicator("TAB1");
th.addTab(specs);
specs = th.newTabSpec("tag2");
specs.setContent(R.id.tab2);
specs.setIndicator("TAB2");
th.addTab(specs);
specs = th.newTabSpec("tag3");
specs.setContent(R.id.tab3);
specs.setIndicator("TAB3");
th.addTab(specs);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bRedColor:
drawview.setColorRed();
break;
}
我在做什麼錯?任何人都可以糾正這個代碼或給我一個設置簡單的應用程序,繪製矩形和能夠通過按鈕更改顏色的適當示例?
謝謝你的回覆。