參考地址: http://www.crifan.com/android_how_to_create_new_ui_and_switch_to_another_new_ui/html
想要實現,在Android的ADT開發環境中,java
在當前界面下,新建一個新的界面,而後從當前界面,切換到新建界面中。android
其中:ios
1. 當前界面是主界面,對應的佈局的xml文件是activity_main.xmlapp
2.新建的一個界面,主要適用於實現文件夾瀏覽方面的功能。dom
Android中,對於界面的控制,是對應的叫作Activity;ide
中文對應含義是 活動。函數
不一樣界面之間的切換過程的控制,包括之間數據的傳遞,叫作Intent;佈局
相似於Mac的iOS開發中的Segue。ui
對應的界面如何佈局,即長啥樣,是對應的layout下面的對應的xml文件決定的;
界面長啥樣,能夠經過Graphical Layout去拖動控件並配置,也能夠本身直接寫xml文件,去配置,效果都是同樣的。
想要切換到另一個界面,那麼要確保新界面可用。
關於,如何從無到有,如何新建一個Activity,能夠參考:
而後接着,總結一下,新增一個Activity,都涉及了哪些改動。
能夠用:
中的這個截圖來總結:
此處會在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
>
|
對應的效果:
增長了一些默認的字符串和標題值:
1
2
|
<
string
name="hello_world">Hello world!</
string
>
<
string
name="title_activity_directory_browser">Directory Browser</
string
>
|
效果:
對應的,添加了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
>
|
效果:
對應的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
>
|
效果是:
在對應的,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
暫時不太清楚這個是啥東東。
不過,也找到了對應的位置,是在libs下面的:
在當前界面中,實現對應的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);
}
}
|
其實,上述全部內容,官網的教程:
基本上都解釋了。只是,沒有本身實踐,是沒法真正理解的。
上面的內容,就是本身折騰過了,才搞清楚的。