Abstract class issue
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();
}
}
}
};
}
You can do it like this,
Your CustomActivity.java
public class CustomActivity extends Activity implements OnClickListener{
public void initLayout(Button button){
button.setOnClickListener(this);
}
public void simple_method(){
System.out.println("test in CustomActivity");
}
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Button Clicked in Custom Activity", Toast.LENGTH_LONG).show();
}
}
Activity that extends CustomActivity CustomClassDemoActivity.java
public class CustomClassDemoActivity extends CustomActivity{
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.myTextView);
button.setText("This is a Custom Activity Example");
super.initLayout(button);
simple_method();
}
public void simple_method(){
super.simple_method();
System.out.println("test in mainClass");
}
@Override
public void onClick(View v) {
super.onClick(v);
Toast.makeText(getApplicationContext(), "Button Clicked in Main Activity", Toast.LENGTH_LONG).show();
}
}
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()
and addCheckChangedListner()
.
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
.
I forgot to call function mappingWidgets()
and addCheckChangedListner()
after calling this two method my code works fine.Thanks all for help me.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome_pillminder);
mappingWidgets();
addCheckChangedListner();
}
精彩评论