Я пытаюсь сделать простое приложение, которое отображает прямоугольник и имеет возможность изменять свой цвет кнопкой.Изменить цвет прямоугольника кнопкой
Прямоугольник класс:
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();
}
Мое приложение Tab Layout приложение. Этот класс отображается в 3-Tab так:
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>
Я пытался получить доступ к setColorRed() из моего класса MainClass.java по
DrawView drawview;
, а затем использовать
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;
}
Что я делаю неправильно? Может ли кто-нибудь исправить этот код или дать мне правильный пример настройки простого приложения, которое рисует прямоугольник и может изменить цвет кнопкой?
Благодарим за внимание.