android-fragments 操作栏检测片段中的后退按钮单击

owfi6suc  于 2022-11-13  发布在  Android
关注(0)|答案(5)|浏览(211)

我有一个片段与行动栏后退按钮启用在它。
编码:

@Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        setHasOptionsMenu(true);
        actionBar = ((MainActivity)getActivity()).getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        actionBar.setCustomView(R.layout.custom_action_bar);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        mInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = mInflater.inflate(R.layout.fragment_layout, container, false);

        return view;
    }

在上面的代码中,actionBar.setDisplayHomeAsUpEnabled(true);启用了操作栏中的后退按钮,但是我们如何检测它的点击呢?
我已经看了很多例子,并尝试了以下,但仍然不工作:
在片段中:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            Toast.makeText(getActivity(), "Back", Toast.LENGTH_LONG).show();
            break;
        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }

此外,在片段中显示的后退按钮是黑色的,我需要白色或自定义颜色,我怎么能改变呢?

j2cgzkjk

j2cgzkjk1#

迟做总比不做好,请试试这个〉

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        mInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = mInflater.inflate(R.layout.fragment_layout, container, false);
        //set setHasOptionsMenu on true here on CreateView method
        setHasOptionsMenu(true);
        return view;
    }

并且实施了

@Override
public boolean onOptionsItemSelected(MenuItem item){
            if (item.getItemId() == android.R.id.home) {
                if (getActivity() != null) {
                    getActivity().onBackPressed();
                }
                return true;
            };
            return super.onOptionsItemSelected(item);
 }
j2qf4p5b

j2qf4p5b2#

我也遇到了同样的问题,但是我把这段代码放在Activity中,返回按钮在片段中工作:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            Toast.makeText(getActivity(), "Back", Toast.LENGTH_LONG).show();
            break;
        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }
izj3ouym

izj3ouym3#

试试这个

@Override
public boolean onOptionsItemSelected(MenuItem item) {
   finish(); //to finish current activity
    return super.onOptionsItemSelected(item);
}
myzjeezk

myzjeezk4#

任何遇到这个问题的人都可以按照下面的步骤操作;
将MenuProvider实作至您的片段类别,并使用MenuHost初始化片段内的选单,然后使用覆写的方法。

class NumbersFragment : Fragment(), MenuProvider{

    private val args: NumbersFragmentArgs by navArgs()

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_numbers, container, false)

        val menuHost: MenuHost = requireActivity()
        menuHost.addMenuProvider(this, viewLifecycleOwner, Lifecycle.State.RESUMED)

        val numberListView = view.findViewById<RecyclerView>(R.id.numbersListView)
        val adapter = NumbersAdapter()
        adapter.setNumberList(args.numberArguments)
        numberListView.adapter = adapter

        return view
    }

    override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
        //Nothing
    }

    override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
        this.findNavController().popBackStack()
        return false
    }
}
fhity93d

fhity93d5#

View cView = getLayoutInflater().inflate(R.layout.header, null);
cView.findViewById(R.id.btn_id).setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
                     // Do stuff here.
    }
});
actionBar.setCustomView(cView);

相关问题