[Android UI design and development] 3. Guiding interface (3) implementing the Guiding interface of an application only once, androidui

Source: Internet
Author: User

[Android UI design and development] 3. Guiding interface (3) implementing the Guiding interface of an application only once, androidui

Most of the boot interfaces are basically the same. As long as you are familiar with one, there is basically nothing to say. To achieve this effect, you only need to start the boot interface once, as long as you use the SharedPreferences class, it will make the program very simple. The following describes the usage of this class in detail.

1. Detailed introduction and usage of SharedPreferences

In fact, I have already introduced the basics of Game Development (game data storage). I 'd like to explain it again for the sake of completeness.

For software development, we should know that many software may have configuration files that store the attribute values of the program running. because there are not many configuration information, it is not cost-effective to store them using databases, because the time consumption of database connection and operation greatly affects the program efficiency, we use the one-to-one correspondence relationship of key values to store the configuration information. SharedPreferences is the technology used in Android to implement the storage method.

SharedPreferences is easy to use and can easily store and read data. SharedPreferences can only store simple types of data, such as String and int. Generally, the data of complex types is converted to Base64 encoding, And the converted data is saved as a string in the XML file, and then saved in SharedPreferences.

SharedPreferences usage:
<1> use the getSharedPreferences method of the Activity class to obtain the SharedPreferences object. The name of the file storing the key-value is specified by the first parameter of the getSharedPreferences method;
<2> use the edit operation of the SharedPreferences interface to obtain the SharedPreferences. Editor object;
<3> use the putXxx method of the SharedPreferences. Editor interface to save the key-value pair. Xxx indicates different data types. For example, the putString method is required for string-type values;
<4> use the commit method of the SharedPreferences. Editor interface to save the key-value pair. The commit method is equivalent to a commit operation in a database transaction.

The code writing process is as follows:


A. Store Data Information
<1> open Preferences with the name setting. If yes, open Preferences. Otherwise, create a new Preferences.

SharedPreferences settings = getSharedPreferences(“setting”, 0);        

<2> set setting to edit

SharedPreferences.Editor editor = settings.edit();

<3> store data

editor.putString(“name”,”cnblogs”);editor.putString(“URL”,”www.cnblogs.com”);

<4> submit

editor.commit();


B. Read data information
<1> Get Preferences

 SharedPreferences settings = getSharedPreferences(“setting”, 0);

<2> retrieve data

String name = settings. getString ("name", "default Value"); String url = setting. getString ("URL", "default ");


The above is how to use SharedPreferences. The storage location of the created Preferences file can be viewed in Eclipse:
DDMS-> File Explorer/data/<package name>/shared_prefs/setting. xml

 

2. Implementation (the pictures are the welcome page, guide page, and software homepage respectively)
Start the program for the first time: welcome page --> Guide page --> Software Homepage
Start the program later: welcome page --> Software Homepage

 

3. Specific implementation Encoding

(1) welcome page Java code, that is, simply create a thread, delay the specified time, and then jump to it.

Package com. yanis. ui; import android. app. activity; import android. content. context; import android. content. intent; import android. content. sharedPreferences; import android. OS. bundle; public class WelcomeActivity extends Activity implements Runnable {public static final int VERSION = 1; public static SharedPreferences sp; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedI NstanceState); setContentView (R. layout. activity_welcome); // start a delay Thread new Thread (this ). start () ;}@ Override public void run () {try {// Thread with a two-second delay. sleep (2000); // read the data sp = getSharedPreferences ("Y_Setting", Context. MODE_PRIVATE);/*** if you are not using it for the first time, directly go to the display interface; otherwise, go to the Guide interface */if (sp. getInt ("VERSION", 0 )! = VERSION) {startActivity (new Intent (WelcomeActivity. this, GuideActivity. class);} else {startActivity (new Intent (WelcomeActivity. this, MainActivity. class) ;}finish () ;}catch (Exception e ){}}}WelcomeActivity

(2) Guide page, which is implemented by ViewPager. For more information, see ViewPager introduction and simple implementation. For details, refer to the source code.

(3) There is nothing to say about the home page. It is a page that displays the layout.

 

Source code: https://github.com/YeXiaoChao/Yc_ui_guideview_once

Source: http://blog.csdn.net/yangyu20121224/article/details/8987342

Related Article

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.