抽象类问题
我正在创建一个父活动(类),然后想将该类扩展到另一个活动(类)。我在所有活动(类)中都有一些控件,所以我决定使用抽象类,这样我就不需要在所有类中编写一些通用代码。我创建了下面的类,其中一个是抽象类。当我调用我的欢迎页面时Activity 这将向我显示一个包含所有常用控件的屏幕(在我的例子中是单选按钮)。在抽象类中,我设置了 checkedChangedListener
侦听器,并在我正在创建的 onCheckedChanged()
方法中祝酒,但事实并非如此显示。在这种情况下我很困惑。不显示 toast 的原因是什么?。
My Activity(Class) Welcomepage_pillReminder 扩展了 CustomTabsActivity
public class Welcomepage_pillReminder extends CustomTabsActivity
@Override
public void mappingWidgets() {
super.mappingWidgets();
}
@Override
public void addCheckChangedListner() {
super.addCheckChangedListner();
}
CustomActivity
public abstract class CustomTabsActivity extends Activity {
protected RadioButton radHome;
public void mappingWidgets(){
radHome = (RadioButton)findViewById(R.id.radHome);
}
public void addCheckChangedListner() {
radHome.setOnCheckedChangeListener(onCheckedChangeListener);
}
OnCheckedChangeListener onCheckedChangeListener = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
if(buttonView.getText().equals("Home")) {
Toast.makeText(getApplicationContext(), "Home", 2000).show();
}
}
}
};
}
I am creating one parent Activity(Class) and then want to extends this class to another Activity(Class). I have some controls in all the Activities(Classes) so I decide to use Abstract class so that I need not write some common code in all the classes.I created below classes and one of it is abstract class.When I am calling my Welcomepage Activity this will display me a screen with all common controls(Radio buttons in my case).In Abstract class I had set checkedChangedListener
listener and in onCheckedChanged()
method I am creating a toast but It is not displaying.I am confused in this case.What is the reason to not displaying a toast?.
My Activity(Class) Welcomepage_pillReminder which extends CustomTabsActivity
public class Welcomepage_pillReminder extends CustomTabsActivity
@Override
public void mappingWidgets() {
super.mappingWidgets();
}
@Override
public void addCheckChangedListner() {
super.addCheckChangedListner();
}
CustomActivity
public abstract class CustomTabsActivity extends Activity {
protected RadioButton radHome;
public void mappingWidgets(){
radHome = (RadioButton)findViewById(R.id.radHome);
}
public void addCheckChangedListner() {
radHome.setOnCheckedChangeListener(onCheckedChangeListener);
}
OnCheckedChangeListener onCheckedChangeListener = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
if(buttonView.getText().equals("Home")) {
Toast.makeText(getApplicationContext(), "Home", 2000).show();
}
}
}
};
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以这样做,
你的 CustomActivity.java
扩展 CustomActivity 的 Activity
CustomClassDemoActivity.java
You can do it like this,
Your CustomActivity.java
Activity that extends CustomActivity
CustomClassDemoActivity.java
出于您的目的,使用抽象类没有意义。
抽象类仅声明其方法,具体类可以根据其目的实现该方法。如果您计划编写各种类来扩展您的活动,并且每个类都有
mappingWidgets()
和addCheckChangedListner()
的另一个实现目的,那么这是有意义的。在您的情况下,一个将被扩展的简单类就可以完成这项工作。
此外,我发现您在子类中使用了 @Override 注释。使用此注释,您可以覆盖您的方法。
首先,请删除子类方法中的
@Override
。我想那时他们会工作的。在使用抽象类和/或
@Override
时也许还要重新考虑。For your purpose the using of an abstract class dosent make sense.
An abstract class only declares its methods and the concrete class can implement the method then depending on its purpose. It would make sense if you plan to write various classes which extend your activity and each of them would have another implementation purpose for
mappingWidgets()
andaddCheckChangedListner()
.In your case a simple class which will be extended would do the job.
Furthermore I see, that you use the
@Override
annotation in your sub class. With this annotation you override your methods.At first, please remove the
@Override
in your methods of the sub class. I guess they will work then.Maybe also reconsider when usin abstract classes and / or
@Override
.在调用这两个方法后,我忘记调用函数
mappingWidgets()
和addCheckChangedListner()
我的代码工作正常。感谢大家的帮助。I forgot to call function
mappingWidgets()
andaddCheckChangedListner()
after calling this two method my code works fine.Thanks all for help me.