本示例使用AndroidSharedPreferences类来保存用户的界面输入,并且使用被保存的输入结果判断要启动的下一个Activity

需要注意的是SharedPreferences类会持久化保存用户的输入,因此如果应用下次启动时不希望保留前次的操作痕迹,应该在应用退出时将本次保存的输入清除。本例中没有进行清除。

1.定义清单文件(AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="my.android.test"

android:versionCode="1"

android:versionName="1.0">

<application android:icon="@drawable/icon" android:label="@string/app_name">

<!-- 应用主Activity -->

<activity android:name=".RedirectEnter"

android:label="@string/app_name">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

<!-- 如果保存了输入痕迹则显示本Activity,否则启动RedirectGetter Activity -->

<activity android:name=".RedirectMain">

</activity>

<!-- 接收用户输入,点击apply按钮时会保存文本框中输入结果 -->

<activity android:name=".RedirectGetter">

</activity>

</application>

<uses-sdk android:minSdkVersion="9" />

</manifest>

2.定义字符资源(stings.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>

<string name="hello">Hello World, RedirectEnter!</string>

<string name="app_name">Redirection</string>

<string name="activity_redirect">App/Activity/Redirection</string>

<string name="redirect_enter">Press the button to start the example. The next activity will conditionally redirect to another activity to collect data from the user.</string>

<string name="redirect_main">You now see the main activity running normally because the user text has been set to:</string>

<string name="clear_text">Clear and Exit</string>

<string name="new_text">New Text</string>

<string name="redirect_getter">Enter the text that will be used by the main activity. Press back to cancel.</string>

<string name="apply">Apply</string>

<string name="go">Go</string>

</resources>

3.定义Activity布局资源

redirect_enter.xml:应用启动界面

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip"

android:gravity="center_horizontal"

android:layout_width="match_parent" android:layout_height="match_parent">

<!-- 用于显示本应用程序的示例说明 -->

<TextView

android:layout_width="match_parent" android:layout_height="wrap_content"

android:layout_weight="0"

android:paddingBottom="4dip"

android:text="@string/redirect_enter"/>

<!-- 启动下一个Activity -->

<Button android:id="@+id/go"

android:layout_width="wrap_content" android:layout_height="wrap_content"

android:text="@string/go">

<requestFocus />

</Button>

</LinearLayout>

redirect_main.xml:重定向判断界面

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip"

android:gravity="center_horizontal"

android:layout_width="match_parent" android:layout_height="match_parent">

<!-- 提示用户,用户已经设置了文本,作为Activity重定向条件 -->

<TextView

android:layout_width="match_parent" android:layout_height="wrap_content"

android:layout_weight="0"

android:paddingBottom="4dip"

android:text="@string/redirect_main"/>

<!-- 显示用户在RedirectGetter界面输入的文本 -->

<TextView android:id="@+id/text"

android:layout_width="match_parent" android:layout_height="wrap_content"

android:layout_weight="0"

android:paddingBottom="4dip" />

<!-- 用户点击该按钮,会清除用户保存的输入,并返回的应用初始界面 -->

<Button android:id="@+id/clear"

android:layout_width="wrap_content" android:layout_height="wrap_content"

android:text="@string/clear_text">

<requestFocus />

</Button>

<!-- 用户点击该按钮时,会返回到文本输入界面,等待用户重新输入 -->

<Button android:id="@+id/newView"

android:layout_width="wrap_content" android:layout_height="wrap_content"

android:text="@string/new_text">

</Button>

</LinearLayout>

redirect_getter:重定向条件输入界面

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip"

android:gravity="center_horizontal"

android:layout_width="match_parent" android:layout_height="match_parent">

<!-- 提示用户在文本域中输入字符 -->

<TextView

android:layout_width="match_parent" android:layout_height="wrap_content"

android:layout_weight="0"

android:paddingBottom="4dip"

android:text="@string/redirect_getter"/>

<!-- 文本输入域 -->

<EditText android:id="@+id/text"

android:layout_width="match_parent" android:layout_height="wrap_content"

android:layout_weight="0"

android:paddingBottom="4dip">

<requestFocus />

</EditText>

<!-- 用户点击该按钮时,会保存文本输入域中的内容,并销毁当前Activity -->

<Button android:id="@+id/apply"

android:layout_width="wrap_content" android:layout_height="wrap_content"

android:text="@string/apply" />

</LinearLayout>

4.编写示例代码

RedirectEnter.java

package my.android.test;

import android.app.Activity;

import android.os.Bundle;

import android.content.Intent;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

publicclass RedirectEnter extends Activity {

/**

* 按钮点击事件监听器,当用户点击go按钮时,会启动重定向界面RedirectMain Activity

*/

private OnClickListener mGoListener = new OnClickListener(){

publicvoid onClick(View v){

//创建一个Intent对象,并启动RedirectMain Activity

Intent intent = new Intent(RedirectEnter.this, RedirectMain.class);

startActivity(intent);

}

};

/** Activity被初次创建时,调用这个方法*/

@Override

publicvoid onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//布局填充

setContentView(R.layout.redirect_enter);

//从布局中查找go按钮,并设置点击事件监听器

Button goButton = (Button)findViewById(R.id.go);

goButton.setOnClickListener(mGoListener);

}

}

RedirectMain.java

package my.android.test;

import android.app.Activity;

import android.content.Intent;

import android.content.SharedPreferences;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.TextView;

publicclass RedirectMain extends Activity {

//启动另一个Activity时的请求代码,区分不同的启动地点

staticfinalintINIT_TEXT_REQUEST = 0;

staticfinalintNEW_TEXT_REQUEST = 1;

//保存从SharedPreferences中取得的文本

private String mTextPref;

/**

* newView按钮监听器,启动RedirectGetter Activity,获取新的输入结果

*/

private OnClickListener mNewListener = new OnClickListener(){

publicvoid onClick(View v){

Intent intent = new Intent(RedirectMain.this, RedirectGetter.class);

startActivityForResult(intent, NEW_TEXT_REQUEST);

}

};

/**

* clear按钮监听器,清除输入结果,并退出当前Activity

*/

private OnClickListener mClearListener = new OnClickListener(){

publicvoid onClick(View v){

SharedPreferences preferences = getSharedPreferences("RedirectData", 0);

preferences.edit().remove("text").commit();

finish();

}

};

/**

* SharedPreferences对象中获取被保存的文本。

* @return true,有被保存的文本;false,没有被保存的文本

*/

privatefinalboolean loadPrefs(){

SharedPreferences preferences = getSharedPreferences("RedirectData", 0);

mTextPref = preferences.getString("text", null);

if(mTextPref != null){

TextView text = (TextView)findViewById(R.id.text);

text.setText(mTextPref);

returntrue;

}

returnfalse;

}

/**

* 覆写ActivityonActivityResult方法,接收该ActivitystartActivityForResult()方法启动的

* 另一个Activity的返回值。

* @param requestCode:启动请求区分代码

* @param resultCode:另一个Activity的返回结果代码

* @param data:另个Activity返回的数据

*/

@Override

protectedvoid onActivityResult(int requestCode, int resultCode, Intent data){

if(requestCode == INIT_TEXT_REQUEST){

if(resultCode == RESULT_CANCELED){

finish();

} else {

loadPrefs();

}

} elseif (requestCode == NEW_TEXT_REQUEST){

if(resultCode != RESULT_CANCELED){

loadPrefs();

}

}

}

/**

* Activity被首次创建时,调用这个方法。

*/

@Override

protectedvoid onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

//填充布局

setContentView(R.layout.redirect_main);

//查找clear按钮,并设置事件监听器

Button clearButton = (Button)findViewById(R.id.clear);

clearButton.setOnClickListener(mClearListener);

//查找newView按钮,并设置事件监听器

Button newButton = (Button)findViewById(R.id.newView);

newButton.setOnClickListener(mNewListener);

//判断用户的输入痕迹,如果没有输入被保存,则启动RedirectGetter Activity,获取用户输入

//否则显示本Activity

if(!loadPrefs()){

Intent intent = new Intent(this, RedirectGetter.class);

startActivityForResult(intent, INIT_TEXT_REQUEST);

}

}

}

RedirectGetter.java

package my.android.test;

import android.app.Activity;

import android.content.SharedPreferences;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.TextView;

publicclass RedirectGetter extends Activity {

//保存界面文本域中输入文本。

TextView mText;

/**

* apply按钮的事件监听器,当用户点击该按钮时,使用SharedPreferences对象保存

* 用户在文本域中的输入结果。

*/

private OnClickListener mApplyListener = new OnClickListener(){

publicvoid onClick(View v){

//获取SharedPreferences对象,并保存文本域中的输入结果。

SharedPreferences preferences = getSharedPreferences("RedirectData", 0);

SharedPreferences.Editor editor = preferences.edit();

editor.putString("text", mText.getText().toString());

if(editor.commit()){

//保存成功,设置保存结果

setResult(RESULT_OK);

}

//退出本Activity

finish();

}

};

/**

* Activity被首次创建时,调用这个方法

*/

@Override

protectedvoid onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

//填充布局

setContentView(R.layout.redirect_getter);

//查找apply按钮,并设置事件监听器

Button applyButton = (Button)findViewById(R.id.apply);

applyButton.setOnClickListener(mApplyListener);

//获取文本输入域的初始值

mText = (TextView)findViewById(R.id.text);

}

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐