Android Studio在fragment中实现点击按钮事件的方法

首先在fragment定义控件,控件名为你给该控件设置的id

    private Button text_to_login;

在这里我给控件定义的id为text_to_login。

然后在onViewCreated方法内添加以下代码:

text_to_login = getActivity().findViewById(R.id.text_to_login);
        text_to_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),login.class);//想调到哪个界面就把login改成界面对应的activity名
                startActivity(intent);
            }
        });

这样就可以实现在fragment中点击按钮跳转到对应activity的事件了。

参考博客:https://blog.csdn.net/m0_53743933/article/details/120676222?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163888864616780264047216%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=163888864616780264047216&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-12-120676222.first_rank_v2_pc_rank_v29&utm_term=fragment%E4%B8%ADbutton&spm=1018.2226.3001.4187

你可能感兴趣的:(android,studio,android,android-studio)