An example of android button click, an example of an Android fragment implementing a button click event in Android application development...

This article will take you to understand the example of the Android application development Android fragment to realize the button click event. I hope this article will be helpful for everyone to learn Android.

Fragment cannot directly perform click events, it needs to be placed in oncreatActivity

code show as below:

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup   container, Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_first, null);

return view;

}

Click event code:

@Override

public void onActivityCreated(@Nullable Bundle   savedInstanceState) {

super.onActivityCreated(savedInstanceState);

Button sweepButton = (Button)   getActivity().findViewById(R.id.image1);

sweepButton.setOnClickListener(new View.OnClickListener()   {

@Override

public void onClick(View v)   {

Toast.makeText(getActivity(),   "nihao", Toast.LENGTH_LONG).show();

//Jump from fragment to activity

startActivity(new Intent(getActivity(),   PayMoneyActivity.class));

}

});

}

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324040910&siteId=291194637