Android中在fragment中实现点击按钮事件

在fragment不能直接进行点击事件,需要放到oncreatActivity中

具体方法如下:

需要注意的是import android.support.v4.app.Fragment;导入的这个包

public class SendFragment extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        return inflater.inflate(R.layout.send_fragment,container,false);
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
       // final User user ;
        final EditText et_title = (EditText) getActivity().findViewById(R.id.et_title);
        final EditText et_salary = (EditText) getActivity().findViewById(R.id.et_salary);
        final EditText et_sex = (EditText) getActivity().findViewById(R.id.et_sex);
        final EditText et_count = (EditText) getActivity().findViewById(R.id.et_count);
        final EditText et_phone = (EditText) getActivity().findViewById(R.id.et_phone);
        final EditText et_time = (EditText) getActivity().findViewById(R.id.et_time);
        final EditText et_address = (EditText) getActivity().findViewById(R.id.et_address);
        final EditText et_description = (EditText) getActivity().findViewById(R.id.et_description);
        Button btn_send = (Button) getActivity().findViewById(R.id.btn_send);

        btn_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //String username=user.getUsername().toString();
                String title = et_title.getText().toString();
                String salary = et_salary.getText().toString();
                String sex = et_sex.getText().toString();
                String count = et_count.getText().toString();
                String phone = et_phone.getText().toString();
                String time = et_time.getText().toString();
                String address = et_address.getText().toString();
                String description = et_description.getText().toString();
                JobDao jobDao = new JobDao(getActivity());
                jobDao.add(new Job(title, salary, sex, count, phone, time, address, description));
                Toast.makeText(getActivity(), "发布成功", Toast.LENGTH_LONG).show();
                Intent intent = new Intent(getActivity(),MainActivity.class);
                startActivity(intent);
            }
        });
    }
}
--------------------- 
作者:shasha1021 
来源:CSDN 
原文:https://blog.csdn.net/shasha1021/article/details/72717229 
版权声明:本文为博主原创文章,转载请附上博文链接!

转载于:https://www.cnblogs.com/derekhan/p/11096235.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值