使用OnClickListener进行Bottombar但没有任何反应

时间:2018-03-21 08:51:08

标签: android android-studio bottombar

我现在非常困惑,因为我需要制作下边栏,但不要总是点击/点按那里的第一个按钮。

这是我的第一次尝试

Powershell.exe -ExecutionPolicy RemoteSigned -File uptime.ps1

这是第二次

bottomBar = (BottomBar)findViewById(R.id.bottombar);
bottomBar.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tab_home:
                Toast.makeText(getBaseContext(),"Destionation",Toast.LENGTH_SHORT).show();
                break;
            case R.id.tab_destiantion:
                Intent intent = new Intent(MainActivity.this, DestinationList.class);
                startActivity(intent);
                break;
            case R.id.tab_counter:
                final AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
                View mView = getLayoutInflater().inflate(R.layout.activity_tipebus, null);
                mBuilder.setTitle("Tipe Bus yang digunakan");
                final  Spinner mSpinner = (Spinner) mView.findViewById(R.id.pilih_spinner);
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,
                        android.R.layout.simple_spinner_item,
                        getResources().getStringArray(R.array.Tipe_Bus));
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                mSpinner.setAdapter(adapter);

                mBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if(mSpinner.getSelectedItem().toString().equalsIgnoreCase("Medium Bus")){
                            startActivity(new Intent(MainActivity.this, MediumBus.class));
                        }else if (mSpinner.getSelectedItem().toString().equalsIgnoreCase("Big Bus")){
                            startActivity(new Intent(MainActivity.this, BigBus.class));
                        }else if (mSpinner.getSelectedItem().toString().equalsIgnoreCase("ELF 12")){
                            startActivity(new Intent(MainActivity.this, Elf12Seat.class));
                        }else if (mSpinner.getSelectedItem().toString().equalsIgnoreCase("ELF 16")){
                            startActivity(new Intent(MainActivity.this, Elf16Seat.class));
                        }
                    }
                });
                mBuilder.setNegativeButton("Batal", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i) {
                        dialog.dismiss();
                    }
                });
                mBuilder.setView(mView);
                AlertDialog dialog = mBuilder.create();
                dialog.show();
                break;
            case R.id.tab_broadcast:
                final AlertDialog.Builder tBuilder = new AlertDialog.Builder(MainActivity.this);
                final View tView = getLayoutInflater().inflate(R.layout.activity_message_passenger,null);
                final EditText editText = (EditText) tView.findViewById(R.id.textHere);
                tBuilder.setTitle("Pesan Anda");
                tBuilder.setPositiveButton("SEND", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        PackageManager pm=getPackageManager();
                        try {
                            Intent waIntent = new Intent(Intent.ACTION_SEND);
                            waIntent.setType("text/plain");
                            String text = editText.getText().toString();
                            //the above (textHere) From my CustomLayout Edit TEXT

                            PackageInfo info = pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
                            //Check if package exists or not. If not then code
                            //in catch block will be called
                            waIntent.setPackage("com.whatsapp");

                            waIntent.putExtra(Intent.EXTRA_TEXT, text);
                            startActivity(Intent.createChooser(waIntent, "Share with"));

                        } catch (PackageManager.NameNotFoundException e) {
                            Toast.makeText(getBaseContext(), "WhatsApp not Installed", Toast.LENGTH_SHORT)
                                    .show();
                        }
                    }
                });
                tBuilder.setNegativeButton("Batal", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i) {
                        dialog.dismiss();
                    }
                });
                tBuilder.setView(tView);
                AlertDialog dialog1 = tBuilder.create();
                dialog1.show();
                break;
            case R.id.tab_note:
                String note = "";
                Intent sendIntent = new Intent(Intent.ACTION_SEND);
                sendIntent.putExtra(Intent.EXTRA_TEXT, note);
                sendIntent.setType("text/plain");

                Intent chooser = Intent.createChooser(sendIntent, "Catatan Perjalanan");
                if (sendIntent.resolveActivity(getPackageManager()) != null){
                    startActivity(chooser);}
                break;
        }
    }
});

第一个代码我跑了什么都没有,按钮没有改变任何东西,但第二个代码让我感到无法想象,因为它总是点击Bottombar上的第一个项目,所以我需要有人来帮助我..谢谢你预先

1 个答案:

答案 0 :(得分:0)

请把这个

bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
        @Override
        public void onTabSelected(@IdRes int tabId) {
            if (tabId == R.id.tab_favorites) {
                // The tab with id R.id.tab_favorites was selected,
                // change your content accordingly.
            }
        }
    });

而不是

bottomBar.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

    }

});