带有 OnClickListener 的底部栏,但没有任何反应
Bottombar with OnClickListener but nothing happen
我现在很困惑,因为我需要制作底部栏,但并不总是 tab/tap 那里的第一个按钮。
这是我的第一次尝试
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.setOnTabSelectListener(new OnTabSelectListener() {
@Override
public void onTabSelected(@IdRes final int tabId) {
if (tabId == R.id.tab_destiantion) {
Intent desintent = new Intent(MainActivity.this, DestinationList.class);
startActivity(desintent);
}else if (tabId == R.id.tab_home) {
Toast.makeText(getBaseContext(),"Beranda",Toast.LENGTH_SHORT).show();
}else if(tabId == 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();
}else if (tabId == 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 dialog = tBuilder.create();
dialog.show();
}else if (tabId == 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);
}
}
}
});
第一个代码我 运行 什么也没有,按钮没有改变任何东西,但是第二个代码让我感到意外,因为它总是点击 Bottombar 上的第一个项目,所以我需要有人帮忙我拜托..提前致谢
请放这个
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) {
}
});
我现在很困惑,因为我需要制作底部栏,但并不总是 tab/tap 那里的第一个按钮。
这是我的第一次尝试
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.setOnTabSelectListener(new OnTabSelectListener() {
@Override
public void onTabSelected(@IdRes final int tabId) {
if (tabId == R.id.tab_destiantion) {
Intent desintent = new Intent(MainActivity.this, DestinationList.class);
startActivity(desintent);
}else if (tabId == R.id.tab_home) {
Toast.makeText(getBaseContext(),"Beranda",Toast.LENGTH_SHORT).show();
}else if(tabId == 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();
}else if (tabId == 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 dialog = tBuilder.create();
dialog.show();
}else if (tabId == 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);
}
}
}
});
第一个代码我 运行 什么也没有,按钮没有改变任何东西,但是第二个代码让我感到意外,因为它总是点击 Bottombar 上的第一个项目,所以我需要有人帮忙我拜托..提前致谢
请放这个
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) {
}
});