抽象类问题

发布于 2024-12-09 19:21:57 字数 1463 浏览 0 评论 0原文

我正在创建一个父活动(类),然后想将该类扩展到另一个活动(类)。我在所有活动(类)中都有一些控件,所以我决定使用抽象类,这样我就不需要在所有类中编写一些通用代码。我创建了下面的类,其中一个是抽象类。当我调用我的欢迎页面时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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凹づ凸ル 2024-12-16 19:21:57

你可以这样做,

你的 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();
    }
}

扩展 CustomActivity 的 Activity
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();
    }
}

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();
    }
}
煮酒 2024-12-16 19:21:57

出于您的目的,使用抽象类没有意义。

抽象类仅声明其方法,具体类可以根据其目的实现该方法。如果您计划编写各种类来扩展您的活动,并且每个类都有 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() 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.

蘑菇王子 2024-12-16 19:21:57

在调用这两个方法后,我忘记调用函数 mappingWidgets()addCheckChangedListner() 我的代码工作正常。感谢大家的帮助。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome_pillminder);
        mappingWidgets();
        addCheckChangedListner();
    }

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();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文