【问题标题】:Android: how to call methods of the activity from a listenerAndroid:如何从侦听器调用活动的方法
【发布时间】:2023-03-08 09:01:01
【问题描述】:

我在我的主要活动中使用BottomBar,我正在关注来自 github 的示例代码,他在其中将侦听器添加到选项卡:

this.bottomBar = (BottomBar) findViewById(R.id.bottomBar);
bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
    @Override
    public void onTabSelected(@IdRes int tabId) {

        switch (tabId) {

            case R.id.tab_evaluate :
                Toast.makeText(getApplicationContext(),"Evaluate",Toast.LENGTH_SHORT).show();
                break;
            case R.id.tab_info:
                Toast.makeText(getApplicationContext(),"INfo",Toast.LENGTH_SHORT).show();
                break;
            default:
                Toast.makeText(getApplicationContext(),"Other",Toast.LENGTH_SHORT).show();
                break;
        }

    }
}); 

它运行良好,我看到了所有的祝酒词。

我的问题:在我的主要活动中,我有两种方法,我想根据所选的选项卡调用:

private void showQrCodeView(){
    this.contentFrame.setVisibility(View.GONE);
    this.qrCodeReaderView.setVisibility(View.VISIBLE);
}

private void hideQrCodeView(){
    this.contentFrame.setVisibility(View.VISIBLE);
    this.qrCodeReaderView.setVisibility(View.GONE);
}

为此,我尝试了这个,它不会引发任何错误但也不起作用。

case R.id.tab_evaluate :
    Toast.makeText(getApplicationContext(),"Evaluate",Toast.LENGTH_SHORT).show();
    MainActivity.this.showQrCodeView();
    break;

我的问题是如何从 OnTabSelectListener 调用我的 MainActivity 方法?

【问题讨论】:

    标签: java android listener bottombar


    【解决方案1】:

    刚刚找出问题所在:方法必须是公开的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-13
      • 2018-07-20
      • 2020-02-28
      • 1970-01-01
      • 1970-01-01
      • 2012-12-10
      • 2012-11-22
      相关资源
      最近更新 更多