【问题标题】:Custom Listener for Compound component复合组件的自定义侦听器
【发布时间】:2016-12-31 03:33:54
【问题描述】:

我编写了一个复合组件,并添加了一个自定义侦听器来响应。

在使用 xml 文件的复合组件的类中。

public class VerticalCounterBlock extends LinearLayout {

    public interface VerticalCounterBlockListener {
        public void onCountChanged(int newCount);
    }

    private VerticalCounterBlockListener mVerticalCounterBlockListener = null;

    public void setVerticalCounterBlockListener(VerticalCounterBlockListener listener){
        mVerticalCounterBlockListener = listener;
    }

    // ... Other functions
}

我得到了我的界面,我得到了监听器,我得到了设置器,我像这样在复合组件中的按钮中使用监听器。当我测试时,我可以看到那里显示的吐司

addBtn = (Button)findViewById(R.id.btn_addcount);
addBtn.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {
        count++;
        counttv.setText(String.format("%1$d", count));
        Toast.makeText(getContext(), "VCB", Toast.LENGTH_SHORT).show();
        if(mVerticalCounterBlockListener != null) {
            mVerticalCounterBlockListener.onCountChanged(count);
        }
    }
});

在我的主要活动中

m20_vcb = (VerticalCounterBlock) findViewById(R.id.vcb_m20);
m20_vcb.setVerticalCounterBlockListener(new VerticalCounterBlock.VerticalCounterBlockListener() {
    @Override
    public void onCountChanged(int newCount) {
        increasePreachCountTotal();
        Toast.makeText(CounterActivity.this, String.format("%1$d", newCount), Toast.LENGTH_SHORT).show();
    }
});

我没有看到那个 toast 也没有参与函数调用。我错过了什么?

【问题讨论】:

    标签: android android-view android-custom-view android-components


    【解决方案1】:

    我可以在这里向您建议几个改进范围,主要是重组当前格式。

    我们不要将接口保留为内部类。所以这是你的VerticalCounterBlockListener.java

    public interface VerticalCounterBlockListener {
        public void onCountChanged(int newCount);
    }
    

    现在在你的MainActivity 中实现这个interface

    public class MainActivity implements VerticalCounterBlockListener {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            m20_vcb = (VerticalCounterBlock) findViewById(R.id.vcb_m20);
            m20_vcb.setVerticalCounterBlockListener(this);
        }
    
        // ... Other methods
    
        // Override the onCountChanged function. 
        @Override
        public void onCountChanged(int newCount) {
            increasePreachCountTotal();
            Toast.makeText(CounterActivity.this, String.format("%1$d", newCount), Toast.LENGTH_SHORT).show();
        }
    }
    

    您可以考虑从addBtn 点击监听器中删除Toast,这可能会产生异常。

    addBtn = (Button)findViewById(R.id.btn_addcount);
    addBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            count++;
            counttv.setText(String.format("%1$d", count));
    
            if(mVerticalCounterBlockListener != null) {
                mVerticalCounterBlockListener.onCountChanged(count);
            }
        }
    });
    

    【讨论】:

    • 所有好的建议。我尝试了你的第一个并使用了一个单独的接口类。这并没有帮助,所以我出于理智和组织偏好而搬回了家。建议 2/3 会导致听众出现问题吗?我只使用了 toast,因为我的调试器坏了并且一直断开连接。
    • 代码段 2 显示在您的 Activity 中保留一个公共侦听器。你也试过了吗?
    • 我确实尝试过,但没有成功。线性布局有什么东西会妨碍操作吗?
    • 这应该可以。但您可以考虑在MainActivity 中添加addBtnonClickListener
    • 它确实有效,我不得不重新启动我的计算机,我认为 android 调试器和界面以及应用程序的覆盖都搞砸了。当我醒来时,我打开电脑并卸载了手机上的应用程序,然后再次尝试,它成功了。
    【解决方案2】:

    这很好,我的系统出了点问题。我取消了应用程序并重新启动计算机,它按预期工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-21
      • 2018-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多