參考地址: 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,可以參考:
然后接着,總結一下,新增一個Activity,都涉及了哪些改動。
新增一個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
>
|
對應的效果:
/res/values/strings.xml
增加了一些默認的字符串和標題值:
1
2
|
<
string
name="hello_world">Hello world!</
string
>
<
string
name="title_activity_directory_browser">Directory Browser</
string
>
|
效果:
/res/menu/activity_directory_browser.xml
對應的,添加了menu下面的xml配置文件,用於表示菜單配置方面的內容:
1
2
3
4
5
6
7
8
9
|
<
item
android:id="@+id/menu_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/menu_settings"/>
</
menu
>
|
效果:
/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
xmlns:android="http://schemas.android.com/apk/res/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"
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
>
|
效果是:
/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:
//
//
NavUtils.navigateUpFromSameTask(
this
);
return
true
;
}
return
super
.onOptionsItemSelected(item);
}
}
|
之所以是放在
/src/crifan/com/downloadsongtastemusic/
下面,那是因為之前自己新建activity時,設置的parent是
crifan.com.DownloadSongtasteMusic
可選:android-support-v4.jar
暫時不太清楚這個是啥東東。
不過,也找到了對應的位置,是在libs下面的:
如何從當前界面,切換到新建的,另外一個界面
當前界面中實現對應的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:
//
//
NavUtils.navigateUpFromSameTask(
this
);
return
true
;
}
return
super
.onOptionsItemSelected(item);
}
}
|
總結
其實,上述所有內容,官網的教程:
基本上都解釋了。只是,沒有自己實踐,是無法真正理解的。
上面的內容,就是自己折騰過了,才搞清楚的。