参考_Android中,如何新建一个界面,并且实现从当前界面切换到到刚才新建的(另外一个)界面 参考地址:

参考地址: http://www.crifan.com/android_how_to_create_new_ui_and_switch_to_another_new_ui/

想要实现,在Android的ADT开发环境中,

在当前界面下,新建一个新的界面,然后从当前界面,切换到新建界面中。

其中:

1. 当前界面是主界面,对应的布局的xml文件是activity_main.xml

2.新建的一个界面,主要适用于实现文件夹浏览方面的功能。

前提知识

Activity

Android中,对于界面的控制,是对应的叫做Activity;

中文对应含义是 活动。

Intent

不同界面之间的切换过程的控制,包括之间数据的传递,叫做Intent;

类似于Mac的iOS开发中的Segue

布局Layout

对应的界面如何布局,即长啥样,是对应的layout下面的对应的xml文件决定的;

界面长啥样,可以通过Graphical Layout去拖动控件并配置,也可以自己直接写xml文件,去配置,效果都是一样的。

详细实现过程

想要切换到另外一个界面,那么要确保新界面可用。

关于,如何从无到有,如何新建一个Activity,可以参考:

【记录】ADT中创建Android的Activity

然后接着,总结一下,新增一个Activity,都涉及了哪些改动。

新增一个Activity所涉及的改动

可以用:

【记录】ADT中创建Android的Activity

中的这个截图来总结:

do changes for activity

AndroidManifest.xml

此处会在AndroidManifest.xml中新增对应的activity配置:

1
2
3
4
5
6
7
8
< 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 >

对应的效果:

AndroidManifest.xml added activity

/res/values/strings.xml

增加了一些默认的字符串和标题值:

1
2
< string  name="hello_world">Hello world!</ string >
< string  name="title_activity_directory_browser">Directory Browser</ string >

效果:

added strings value

/res/menu/activity_directory_browser.xml

对应的,添加了menu下面的xml配置文件,用于表示菜单配置方面的内容:

1
2
3
4
5
6
7
8
9
< menu  xmlns:android="http://schemas.android.com/apk/res/android" >
 
     < item
         android:id="@+id/menu_settings"
         android:orderInCategory="100"
         android:showAsAction="never"
         android:title="@string/menu_settings"/>
 
</ menu >

效果:

menu settings

/res/layout/activity_directory_browser.xml

对应的layout布局中,必须也要生成对应的配置。

我此处,是另外,借用了别人的配置,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<!--
<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_content"
         android:layout_centerHorizontal="true"
         android:layout_centerVertical="true"
         android:text="@string/hello_world" />
         </RelativeLayout>
-->
 
< LinearLayout
   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"
     android: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_width="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"
     />
     < Button    
     android:id="@+id/buttonCancle" 
     android:layout_width="125dp"   
     android:layout_height="fill_parent"   
     android:text="Cancel" 
     />
   </ LinearLayout >
</ LinearLayout >

效果是:

layout xml ui

/src/crifan/com/downloadsongtastemusic/DirectoryBrowser.java

在对应的,domain下,新增了对应的此java函数,用于实现,对应的所有的逻辑,默认是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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 {
 
     @Override
     protected  void  onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_directory_browser);
         // Show the Up button in the action bar.
         getActionBar().setDisplayHomeAsUpEnabled( true );
     }
 
     @Override
     public  boolean  onCreateOptionsMenu(Menu menu) {
         // Inflate the menu; this adds items to the action bar if it is present.
         getMenuInflater().inflate(R.menu.activity_directory_browser, menu);
         return  true ;
     }
 
     @Override
     public  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);
     }
 
}

之所以是放在

/src/crifan/com/downloadsongtastemusic/

下面,那是因为之前自己新建activity时,设置的parent是

crifan.com.DownloadSongtasteMusic

可选:android-support-v4.jar

暂时不太清楚这个是啥东东。

不过,也找到了对应的位置,是在libs下面的:

android support v4 jar

如何从当前界面,切换到新建的,另外一个界面

当前界面中实现对应的Indent

在当前界面中,实现对应的Indent,表示要实现的是界面切换:

在我此处的src/crifan/com/downloadsongtastemusic/MainActivity.java中某个函数中实现了:

1
2
3
4
5
6
/** Choose folder for downloaded music file to save */
public  void  ChooseFoler(View view)
{
     Intent intent = new  Intent(MainActivity. this , DirectoryBrowser. class ); 
     startActivityForResult(intent, FOLDER_RESULT_CODE);
}

注:

1. 其中此处用的是startActivityForResult,表示的是,启动一个Indent,(同时是要等新界面中返回来结果的)

所以,此处才需要另外再实现,获得了返回的结果后的处理:

1
2
3
4
5
6
7
8
9
10
@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.如果你无需获得返回值,那么只需要使用startActivity:

1
startActivity(intent);

在另外一个,新界面中,实现对应的初始化功能

就是在对应的onCreate中,做自己需要做的初始化的事情。

默认是的:

1
2
3
4
5
     super .onCreate(savedInstanceState);
     setContentView(R.layout.activity_directory_browser);
     // Show the Up button in the action bar.
     getActionBar().setDisplayHomeAsUpEnabled( true );
}

此处,可以根据自己需要,改为自己要的功能。比如此处文件夹浏览,就是,借鉴了别人的代码,写成类似于:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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 ;
     
     @Override
     protected  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);
     }
     
     @Override
     public  boolean  onCreateOptionsMenu(Menu menu) {
         // Inflate the menu; this adds items to the action bar if it is present.
         getMenuInflater().inflate(R.menu.activity_directory_browser, menu);
         return  true ;
     }
 
     @Override
     public  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);
     }
 
}

总结

其实,上述所有内容,官网的教程:

Starting Another Activity

基本上都解释了。只是,没有自己实践,是无法真正理解的。

上面的内容,就是自己折腾过了,才搞清楚的。

猜你喜欢

转载自www.cnblogs.com/Alex80/p/11263089.html