Tôi có một đoạn hộp thoại trong đó tôi có thanh công cụ trong bố cục. Tôi muốn tạo nút quay lại (Biểu tượng Điều hướng) hoạt động trên thanh công cụ và thoát khỏi đoạn khi được nhấp. Nhưng tôi không thể nắm bắt sự kiện nhấp chuột trên biểu tượng điều hướng của thanh công cụ trong đoạn (hộp thoại).Làm cách nào để bắt biểu tượng điều hướng, nhấp vào thanh công cụ từ phân đoạn?
Sau đây là cách tôi nhận được thanh công cụ:
toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
toolbar.setTitle(itemType);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
Dưới đây là file layout của tôi cho các đoạn thoại:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/panel_cyan"
android:id="@+id/rootLayout"
>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="@color/color_primary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listViewItems"
/>
</RelativeLayout>
** Dưới đây là những gì đã cố gắng cho đến nay nhưng thất bại **
Tùy chọn nhấp chuột vào mục R.id.home
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case android.R.id.home:
finish();
break;
}
return super.onOptionsItemSelected(item);
}
setNavigationOnClick() trên thanh công cụ:
toolbar.setNavigationOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
Toast.makeText(getActivity(), "Back clicked!", Toast.LENGTH_SHORT).show();
}
});
bạn có 'setHasOptionsMenu (true);' trong đoạn của bạn không? – Pr38y
Có. Tôi có setHasOptionsMenu (true); trong onCreate() – priyankvex
Nếu bạn đã setHasOptionMenu true và bạn đang ghi đè 'onCreateOptionsMenu' với super call thì' onOptionsItemSelected' sẽ được kích hoạt. Bạn có thể vui lòng đặt điểm gỡ lỗi trong 'onOptionsItemSelected' và kiểm tra xem nó có được gọi không? – Pr38y