main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    >  
<TextView     
    android:id="@+id/title"  
    android:layout_width="fill_parent"    
    android:layout_height="wrap_content"    
    android:text="----"  
    />  
<com.myTextViewDemo.MyTextView  
    android:id="@+id/text"  
    android:layout_width="fill_parent"    
    android:layout_height="wrap_content"    
/>  
<Button  
    android:id="@+id/btn"  
    android:layout_width="fill_parent"    
    android:layout_height="wrap_content"    
    android:text="change"  
/>  
</LinearLayout>  
public class MyTextViewDemo extends Activity {   
    private MyTextView mt;   
    private Button btn;   
    private TextView tv;   
    private int i = 0;   
    @Override  
    public void onCreate(Bundle savedInstanceState) {   
        super.onCreate(savedInstanceState);   
        setContentView(R.layout.main);   
        mt = (MyTextView) findViewById(R.id.text);   
        tv = (TextView) findViewById(R.id.title);   
        btn = (Button) findViewById(R.id.btn);   
        btn.setOnClickListener(new Button.OnClickListener() {   
            @Override  
            public void onClick(View v) {   
                tv.setText("adsf:" + i++);   
            }   
        });   
    }   
}  
自定义TextView
public class MyTextView extends TextView {   
  
    public MyTextView(Context context) {   
        super(context);   
    }   
  
    public MyTextView(Context context, AttributeSet attrs) {   
        super(context, attrs);   
    }   
  
    public MyTextView(Context context, AttributeSet attrs, int defStyle) {   
        super(context, attrs, defStyle);   
    }   
  
    @Override  
    protected void onDraw(Canvas canvas) {   
        System.out.println("--------------- onDraw --------------");   
        super.onDraw(canvas);   
    }   
}  现在的问题是,当点击button时,会改变tv的值,但是(MyTextView)mt .onDraw也会被调用,而且点击几次就调用几次,这是为什么??

解决方案 »

  1.   

    按一下按钮整个界面都要刷新的,所以就调用了那个onDraw
      

  2.   

    是不是只要改变界面上 非自定义控件 的属性,整个界面都会更新???如果是这样:
    那tv控件如果是类似秒表功能,tv显示内容一直在变,那mt.onDraw也就一直在调用了??
      

  3.   

    系统的设计是,焦点改变时,View可以显示失去焦点的样子。
    上述情况里,mt的焦点改变(应该是失去)了,所以系统要求其重绘,以显示改变焦点后的样子。
      

  4.   

    遇到同样的问题,不过我的没有执行onDraw()函数。求解!!!