Android复合组件的访问方法
我定义了一个复合组件,它由两个Android复合组件的访问方法,android,view,methods,components,Android,View,Methods,Components,我定义了一个复合组件,它由两个TextViews和一个EditText组成。在这个复合组件的类中,我定义了一个getter方法,它返回EditText视图,这样我就可以在活动的onCreate方法中将OnFocusChangeListener设置为这个EditText 但是,在onFocusChange方法中,我需要访问在复合组件类中定义的一些方法,但我所拥有的只是视图对象。可以在复合组件类中访问这些方法吗?也许我应该重新思考代码的结构 复合构件类(为简洁起见进行了简化): 活动: public
TextView
s和一个EditText
组成。在这个复合组件的类中,我定义了一个getter方法,它返回EditText
视图,这样我就可以在活动的onCreate
方法中将OnFocusChangeListener
设置为这个EditText
但是,在onFocusChange
方法中,我需要访问在复合组件类中定义的一些方法,但我所拥有的只是视图对象。可以在复合组件类中访问这些方法吗?也许我应该重新思考代码的结构
复合构件类(为简洁起见进行了简化):
活动:
public class MyActivity extends Activity implements View.OnFocusChangeListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
CompoundComponent cc1 = (CompoundComponent) findViewById(R.id.myComponent1);
cc1.getEditText().setOnFocusChangeListener(this);
CompoundComponent cc2 = (CompoundComponent) findViewById(R.id.myComponent2);
cc2.getEditText().setOnFocusChangeListener(this);
}
public void onFocusChange(View v, boolean focus) {
// need to invoke cc's methods here, i.e. for cc1 or cc2
}
}
如果有任何提示,我将不胜感激。谢谢。制作
cc1
和cc2
活动字段,并在CompoundComponent
中定义所需的任何方法,谢谢。但是我如何知道在主活动的onFocusChange
中调用cc1
还是cc2
?i、 例如,当cc1
或cc2
失去焦点时调用onFocusChange;我如何知道事件发生在哪一个?public void onFocusChange(视图v,布尔焦点){CompoundComponent cc=(CompoundComponent)v;cc.doSomething();}谢谢,但计算机说不。CompoundComponent不仅仅是一个编辑文本。它有其他视图对象(例如TextView
),因此我不能将v
,即EditText
,转换为CompoundComponent
。嗯……然后将视图传递给CompoundComponent,并在其中进行比较。您可以先传递到一个CompoundView,然后再传递到另一个。其中一个会说这是她的观点。
public class MyActivity extends Activity implements View.OnFocusChangeListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
CompoundComponent cc1 = (CompoundComponent) findViewById(R.id.myComponent1);
cc1.getEditText().setOnFocusChangeListener(this);
CompoundComponent cc2 = (CompoundComponent) findViewById(R.id.myComponent2);
cc2.getEditText().setOnFocusChangeListener(this);
}
public void onFocusChange(View v, boolean focus) {
// need to invoke cc's methods here, i.e. for cc1 or cc2
}
}