当findsellectedtBt()在findSellectedBt()中调用invalidate时不起作用。 所以onDraw()不会被调用。
为什么这些代码无法正常工作?
public class CustomView extends View {
private Typeface t;
private Paint paint;
private Buttons sellectedBt;
public CustomView(Context context) {
... }
public CustomView(Context context, AttributeSet attrs) {
...}
public CustomView(Context context, AttributeSet attrs, int defStyle) {
...}
@Override
protected void onDraw(Canvas canvas) {
if(isInEditMode()) return ;
canvas.drawColor(Color.YELLOW);
switch(sellectedBt) {
case ID:
t = Typeface.create(Typeface.DEFAULT, Typeface.NORMAL);
paint.setTypeface(t);
canvas.drawText("ID: 0000", 50, 100, paint);
case PHOTO:
break;
case LINE:
break;
default:
break;
}
}
public void findSellectedBt(Buttons buttonId ) {
sellectedBt = buttonId;
invalidate();
}
}
公共类MainActivity扩展了Activity {
private CustomView customView;
private Button btId;
private CheckBox btPhoto;
private ToggleButton btLineOff;
private Button btClaer;
private ToggleButton btBlue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
customView = new CustomView(this);
btId = (Button)findViewById(R.id.id);
btPhoto = (CheckBox)findViewById(R.id.photo);
btId.setOnClickListener(new OnClickListener() {
public void onClick(View v){
System.out.println("idOnclick");
customView.findSellectedBt(Buttons.ID);
}
});
btPhoto.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
System.out.println("photoChecked");
if(arg1 == true)
customView.findSellectedBt(Buttons.PHOTO);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
package com.example.hw4_200921275;
public enum Buttons {ID,PHOTO,LINE,CLEAR,BLUE}
答案 0 :(得分:0)
仔细检查你在哪里打电话
findSellectedBt(buttonsObject);
顺便提一下,注意Sellected
中的拼写错误。