Android onFinishInflate方法如何使用
在Android中,onFinishInflate方法是View类中的一个方法,用于在View被inflate(填充)后调用。具体使用方法以下:
public class CustomView extends View {
// ...
public CustomView(Context context) {
super(context);
// ...
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
// ...
}
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// ...
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
// 在这里进行View的初始化工作,比如查找子View,设置监听器等
// ...
}
// ...
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
// 查找子View
TextView textView = findViewById(R.id.text_view);
// 设置监听器
textView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 处理点击事件
}
});
}
注意事项:
TOP