Refer to _android, how to create a new interface, and to switch from the current interface to the newly created (another) interface

Source: Internet
Author: User

Reference Address: http://www.crifan.com/android_how_to_create_new_ui_and_switch_to_another_new_ui/

To achieve this, in the Android ADT Development environment,

Under the current interface, create a new interface and switch to the new interface from the current interface.

which

1. The current interface is the main interface, the corresponding layout of the XML file is Activity_main.xml

2. A new interface, which is mainly used to realize the function of folder browsing.

Prerequisite KnowledgeActivity

Android, for the interface control, is the corresponding call activity;

Chinese correspondence meaning is activity.

Intent

The control of the switching process between different interfaces, including the transfer of data between them, is called intent;

Segue in iOS development similar to Mac.

layout Layouts

The corresponding interface how to layout, that is, the length of the corresponding layout of the corresponding XML file is determined;

Interface long what kind, can be through graphical layout to drag control and configuration, you can also directly write XML files, to configure, the effect is the same.

Detailed implementation Process

To switch to another interface, make sure the new interface is available.

About, how to create a new activity from scratch, you can refer to:

Create Android activity in "record" ADT

Then, to summarize, what changes are involved in adding an activity.

the changes involved in adding an activity

Can be used:

Create Android activity in "record" ADT

In this to summarize:

Androidmanifest.xml

The corresponding activity configuration is added here in Androidmanifest.xml:

        <activity            android:name= "Crifan.com.downloadsongtastemusic.DirectoryBrowser"            android:label= "@string/ Title_activity_directory_browser "            android:parentactivityname=" com.crifan.DownloadSongtaste.MainActivity " >            <meta-data                android:name= "Android.support.PARENT_ACTIVITY"                android:value= " Com.crifan.DownloadSongtaste.MainActivity "/>        </activity>

The corresponding effect:

/res/values/strings.xml

Some default string and caption values are added:

    <string name= "Hello_world" >hello world!</string>    <string name= "Title_activity_directory_ Browser ">directory browser</string>

Effect:

/res/menu/activity_directory_browser.xml

Corresponding to the menu, the following XML configuration file is added, which is used to represent the contents of the menus configuration:

<menu xmlns:android= "Http://schemas.android.com/apk/res/android" >    <item        android:id= "@+id/menu_ Settings "        android:orderincategory=" "        android:showasaction=" never "        android:title=" @string/menu_ Settings "/></menu>

Effect:

/res/layout/activity_directory_browser.xml

Corresponding layout must also be generated for the corresponding configuration.

Here I am, in addition, borrowed someone else's configuration, as follows:

<!--<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "/HTTP/ Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Tools:context =". Directorybrowser "> <textview android:layout_width=" wrap_content "android:layout_height=" Wrap_conte NT "android:layout_centerhorizontal=" true "android:layout_centervertical=" true "android:text=" @string /hello_world "/> </relativelayout>--><linearlayout xmlns:android=" http://schemas.android.com/apk/r Es/android "android:layout_width=" 250DP "android:layout_height=" 400DP "android:orientation=" vertical "> < TextView android:id= "@+id/mpath" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" and     roid:padding= "5DP" android:textsize= "18SP" > </TextView> <listview android:id= "@android: Id/list" Android:layout_width= "Fill_parent" android:layout_height= "330DP" > </ListView> <linearlayout android:gravity= "center" android:layout_w Idth= "Wrap_content" android:layout_height= "wrap_content" android:orientation= "Horizontal" ><Button android: Id= "@+id/buttonconfirm" android:layout_width= "125DP" android:layout_height= "fill_parent" android:text= "OK"/&G    T <button android:id= "@+id/buttoncancle" android:layout_width= "125DP" android:layout_height= "Fill_par Ent "android:text=" Cancel "/> </LinearLayout></LinearLayout>

The effect is:

/src/crifan/com/downloadsongtastemusic/directorybrowser.java

In the corresponding, domain, add the corresponding Java function, for implementation, corresponding to all the logic, the default is:

Package Crifan.com.downloadsongtastemusic;import Android.os.bundle;import Android.app.activity;import Android.view.menu;import Android.view.menuitem;import Android.support.v4.app.navutils;public Class Directorybrowser extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_directory_browser);//Show the UP button in the action Bar.getactionbar (). Setdisplayhomeasupenabled (True); @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.activity_directory_browser, menu); return true;} @Overridepublic boolean onoptionsitemselected (MenuItem item) {switch (Item.getitemid ()) {case Android. r.id.home://This ID represents the home or up button. In the case of this//activity, the "Up" button is shown. Use Navutils to allow users//to navigate up one level in the application structure. for//more details, seE The Navigation pattern on Android design:////http://developer.android.com/design/patterns/navigation.html# Up-vs-back//navutils.navigateupfromsametask (this); return true;} return super.onoptionsitemselected (item);}}

The reason is placed in

/src/crifan/com/downloadsongtastemusic/

This is because when you create a new activity, the parent that you set is

Crifan.com.DownloadSongtasteMusic

Optional: Android-support-v4.jar

It's not clear what this is.

However, also found the corresponding position, is below the Libs:

How to switch from the current interface to the new one, another interfaceimplement the corresponding indent in the current interface

In the current interface, the corresponding indent is implemented, which means that the interface switch is implemented:

In my src/crifan/com/downloadsongtastemusic/mainactivity.java here, I implemented a function that:

    /** Choose folder for downloaded music file to save * * Public    void Choosefoler (view view)    {        Intent Intent = NE W Intent (Mainactivity.this, directorybrowser.class);          Startactivityforresult (Intent, Folder_result_code);    }

Note:

1. The Startactivityforresult is used here, which means to start a indent (and to wait for the result to be returned in the new interface)

Therefore, it is only necessary to implement this again, and to obtain the result of the return processing:

    @Override      protected void onactivityresult (int requestcode, int resultcode, Intent data) {          if (folder_result_ CODE = = Requestcode) {            bundle bundle = NULL;            if (data!=null&& (Bundle=data.getextras ())!=null) {            EditText Etsaveto = (EditText) Findviewbyid ( R.id.saveto);            Etsaveto.settext (bundle.getstring ("file"));}}}    

2. If you do not need to get a return value, you only need to use startactivity:

StartActivity (Intent);

in another, new interface, the corresponding initialization function is implemented

is in the corresponding OnCreate, do what you need to do the initialization of things.

Default Yes:

Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_directory_browser);//Show the up button in the Action Bar.getactionbar (). Setdisplayhomeasupenabled (True);

Here, you can change the function you want, depending on your needs. For example, folder browsing here, that is, drawing on someone else's code, written similar to:

Package Crifan.com.downloadsongtastemusic;import Java.io.file;import Java.util.arraylist;import java.util.List; Import Android.os.bundle;import android.os.environment;import android.view.menu;import Android.view.MenuItem; Import Android.widget.arrayadapter;import Android.support.v4.app.navutils;//import Android.app.activity;import Android.app.listactivity;public class Directorybrowser extends Listactivity {//public class Directorybrowser extends    Activity {private list<string> items = null; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_directory_browser);//Show the UP button in the Action Bar.//getactionbar (). Setdisplayhomeasupenabled (True); GetFiles (New File (Environment.getexternalstoragedirectory (). GetAbsolutePath () + "    /"). Listfiles ());}        private void GetFiles (file[] files) {items = new arraylist<string> ();        Items.Add (getString (r.string.goto_root)); For (File file: files) {Items.Add (File.getpath ());        } arrayadapter<string> fileList = new Arrayadapter<string> (this,r.layout.file_list_row, items);    Setlistadapter (fileList); } @Overridepublic Boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action Bar if it is P Resent.getmenuinflater (). Inflate (R.menu.activity_directory_browser, menu); return true;} @Overridepublic boolean onoptionsitemselected (MenuItem item) {switch (Item.getitemid ()) {case Android. r.id.home://This ID represents the home or up button. In the case of this//activity, the "Up" button is shown. Use Navutils to allow users//to navigate up one level in the application structure. for//more details, see the Navigation-pattern on Android design:////http://developer.android.com/design/patterns/ Navigation.html#up-vs-back//navutils.navigateupfromsametask (this); return true;} return super.onoptionsitemselected (item);}}

Summary

In fact, all the above content, the official website of the tutorial:

Starting another Activity

Basically, it's all explained. Just, without their own practice, is unable to really understand.

The above content, is the oneself to toss over, only then to understand.

Refer to _android, how to create a new interface, and to switch from the current interface to the newly created (another) interface

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.