Android sharepreference data storage stores data in the input box, And sharepreference stores data

Source: Internet
Author: User

Android sharepreference data storage stores data in the input box, And sharepreference stores data

Fragment uses a lightweight data storage sharepreference, and the code is clear. Save the data in the input box and click Save.

Simple and clear code for personal projects:

For more information, see:

Class inherits the click event of Fragment implementation:

Public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {// TODO Auto-generated method stub View = inflater. inflate (R. layout. wifi, container, false); view. findViewById (R. id. openwifi ). setOnClickListener (this); view. findViewById (R. id. closewifi ). setOnClickListener (this); wifiurl = (EditText) view. findViewById (R. id. editText1); wificom = (EditText) view. findViewById (R. id. editText2); wificamera = (EditText) view. findViewById (R. id. editText3); // This code source personal blog: http://www.cnblogs.com/xiaobo-Linux/ wavelet Linux qq 463431476 Zhao Archive

SharedPreferences = getActivity (). getSharedPreferences ("wifisetparameters", Activity. MODE_PRIVATE); // private data; String str_wifiurl = sharedPreferences. getString ("wifiurl", "192.168.8.1"); String str_wificom = sharedPreferences. getString ("wificom", "2016"); String str_wificamera = sharedPreferences. getString ("wificamera", "8083"); wifiurl. setText (str_wifiurl); wificom. setText (str_wificom); wificamera. setText (str_wificamera); return view ;}

This code source personal blog: http://www.cnblogs.com/xiaobo-Linux/ wavelet Linux qq 463431476 Zhao Archive

Save data:

 

Public void savewifisettings () {sharedPreferences = getActivity (). getSharedPreferences ("wifisetparameters", Activity. MODE_PRIVATE); // private data; Editor editor = sharedPreferences. edit (); // get the editor. putString ("wifiurl", wifiurl. getText (). toString (); editor. putString ("wificom", wificom. getText (). toString (); editor. putString ("wificamera", wificamera. getText (). toString (); editor. commit (); // submit changes}


Button save:

 

@ Override public void onClick (View v) {// TODO Auto-generated method stub switch (v. getId () {case R. id. openwifi: savewifisettings (); // Save the settings when enabling wifi }}

This code source personal blog: http://www.cnblogs.com/xiaobo-Linux/ wavelet Linux qq 463431476 Zhao Archive

View xml data:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.