android頁面跳轉與傳值
① 如何實現android跳轉頁面並傳遞參數
activity間的傳值
1.值由A.class傳遞到B.class
A.class中:
B.class中:
Intent intent = getIntent();
//獲取數據
String username = intent.getStringExtra("username1");
String userpwd = intent.getStringExtra("userpwd1");
/* Bundle data = intent.getExtras();
String username = intent.getString("username1");
String userpwd = intent.getString("userpwd1"); */
2.除了A.class可以向B.class傳值外,B.class也可以返回值
A.class中
this.startActivity(intent);
改為this.startActivityFroResult(intent,1);//1為請求碼
B.class中
對傳過來的intent對象賦新值
intent.putExtra("username2",username2);
intent.putExtra("userpwd2",userpwd2);
this.setResult(1,intent);
this.finish();//結束焦點
A.class中重寫
@Override protected void onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult(requestCode,resultCode,data);
if(resultCode==1)
{
//可從data中取出值
}
}
A.class中取出B.class中intent傳過來的值
3.intent.setClass(A.this,B.class)的另一種寫法
在manifest.xml中B的Activity中加入
<intent-filter>
<action android:name="com.showB">//這里可以隨便寫
<category android:name = "android.intent.category.DEFAULT">
</intent-filter>
那麼A中就可以直接寫
intent.setAction("com.showB");
來代替
intent.setClass(A.this,B.class);
這也就提示了我們利用intent-filter可以實現其他很多功能
② android intent跳轉怎樣傳輸大數據
在Activity或者組件之前傳遞信息時,一般採用intent綁定bundle的方式傳值,但在使用過程中需要注意的是不要用bundle傳遞大容量數據:
在做項目的過程中,需要將聽寫界面的聽寫結果信息傳遞到聽寫記錄界面供顯示用,但是由於傳遞的數據量過大導致程序ANR,甚至直接報異常(傳遞的信息裡面有bitmap轉換成的byte數組、每一個片語的拼音、詞語、語音信息),經過分析發現是由於bundle不能傳遞大容量的數據信息,在stackoverflow裡面查閱發現有同行遇到類似的問題:
(1)「The size limit of Intent is still pretty low in Jelly Bean, which is somewhat lower than 1MB (around 90K), so you should always be cautious about your data length, even if your application targets only latest Android versions.」
(2)「As per my experience (sometime ago), you are able to put up to 1MB of data in a Bundleencapsulated inside Intent. I think, this restriction was valid up till Froyo or GingerBread.」
所以在通過bundle傳遞數據時只能傳遞較小的數據信息,對於在不同組件之間需要傳遞大容量數據的情況時,有幾種替代方式可以解決不能用bundle傳遞這些數據的問題:
方法一:將需要傳遞的數據寫在臨時文件或者資料庫中,再跳轉到另外一個組件的時候再去讀取這些數據信息,這種處理方式會由於讀寫文件較為耗時導致程序運行效率較低;
方法二:將需要傳遞的數據信息封裝在一個靜態的類中(注意當前組件和要跳轉到的組件必須屬於同一個進程,因為進程之間才能夠共享數據),在當前組件中為類設置內容,然後再跳轉到的組件中去取,這種處理方式效率很高,但是會破壞程序的獨立性。
具體採用哪種替代方式取決於具體的情況,本人建議採取第二種處理方式,因為這樣會大大提高程序的運行效率,至於程序的獨立性,看你怎麼去封裝這個類了。
③ Android 頁面之間數據傳遞方式有幾種各有何優勢
如果頁面之間有直接關系,如Activity和在它之內的Fragment,可以直接通過介面的調用來傳遞數據。優勢:直接,方便。 劣勢:代碼耦合性較高
如果是兩個Activity之間傳遞數據,有界面切換的過程的話,可以用startActivity
或startActivityForResult。用其中的intent參數攜帶數據。優勢:一般用於初始化Activity和調用系統功能如果頁面之間傳遞數據沒有頁面切換的過程,可以通過廣播的方式,sendBroadcast(intent);
要接受數據的頁面注冊這個廣播就行了。
優勢:代碼耦合性低,易重構,適用范圍廣。缺點:數據需要序列化和反序列化,代碼較多
4.通過存儲介質來分享數據,如頁面A將數據存入資料庫,SharedPreferences
,文件,Internet。頁面B通過讀取它們來得到數據
優勢:數據保存時間長,不受到界面生命周期的影響 缺點:讀取速度較慢,需要非同步操作
5。採用事件匯流排的方式,注冊和接收事件(數據),其中的代表者是EventBus,頁面需要指定和注冊接收事件的類型
優勢:不用序列化數據,適用范圍大 缺點:需要學習使用,
④ 安卓中如何實現頁面跳轉
安卓實現頁面跳轉及傳遞參數教程:
用類名跳轉
Intent負責對應用中一次操作的動作、動作涉及數據、附加數據進行描述,Android則根據此Intent的描述, 負責找到對應的組件,將 Intent傳遞給調用的組件,並完成組件的調用。Intent在這里起著實現調用者與被調用者之間的解耦作用。
Intent傳遞過程中,要找到目標消費者(另一個Activity,IntentReceiver或Service),也就是Intent的響應者。
java">Java代碼packagecom.Android;
importandroid.app.Activity;
importandroid.content.Intent;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.view.View.OnClickListener;
{
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.formstuff);
finalImageButtonbutton=(ImageButton)findViewById(R.id.android_button);
button.setOnClickListener(newOnClickListener(){
publicvoidonClick(Viewv){
//用類名跳轉,需要在AndroidManifest.xml中申明activity
Intentintent=newIntent(FormStuff.this,HelloTabWidget.class);
startActivity(intent);
}
});
}
復制代碼Xml代碼<?xmlversion="1.0"encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="com.Android"android:versionCode="1"android:versionName="1.0">
<applicationandroid:icon="@drawable/icon"android:theme="@android:style/Theme.NoTitleBar">
<activityandroid:name=".FormStuff"android:label="@string/app_name">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>
<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!--申明activity-->
<activityandroid:name="HelloTabWidget"></activity>
</application>
<uses-sdkandroid:minSdkVersion="4"/>
</manifest>使用Action跳轉實現
使用Action跳轉,如果有一個程序的 AndroidManifest.xml中的某一個Activity的IntentFilter段中定義了包含了相同的Action那麼這個Intent 就與這個目標Action匹配。如果這個IntentFilter段中沒有定義 Type,Category,那麼這個 Activity就匹配了。但是如果手機中有兩個以上的程序匹配,那麼就會彈出一個對話可框來提示說明。
Action的值在Android中有很多預定義,如果想直接轉到你自己定義的Intent接收者,可以在接收者的 IntentFilter中加入一個自定義的Action值(同時要設定 Category值為"android.intent.category.DEFAULT"),在Intent中設定該值為Intent的 Action,就直接能跳轉到自己的Intent接收者中。因為這個Action在系統中是唯一的。
data/type,可以用Uri來做為data,比如Uri uri = Uri.parse(http://www.google.com);
Intent i = new Intent(Intent.ACTION_VIEW,uri);手機的Intent分發過程中,會根據http://www.google.com 的scheme判斷出數據類型type
手機的Brower則能匹配它,在Brower的Manifest.xml中的IntenFilter中首先有ACTION_VIEW Action,也能處理http:的type。
至於分類Category,一般不要去在Intent中設置它,如果寫Intent的接收者,就在Manifest.xml的 Activity的 IntentFilter中包含android.category.DEFAULT,這樣所有不設置 Category(Intent.addCategory(String c);)的Intent都會與這個Category匹配。
extras(附加信息),是其它所有附加信息的集合。使用extras可以為組件提供擴展信息,比如,如果要執行「發送電子郵件」這個動作,可以將電子郵件的標題、正文等保存在extras里,傳給電子郵件發送組件。
Java代碼packagecom.android.edit_text;
importandroid.app.Activity;
importandroid.content.Intent;
importandroid.os.Bundle;
importandroid.view.KeyEvent;
importandroid.view.View;
importandroid.widget.EditText;
{
privateTextViewm_TextView;
privateEditTextm_EditText;
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
m_EditText=(EditText)this.findViewById(R.id.EditText01);
m_EditText.setOnKeyListener(editTextKeyListener);
}
privateEditText.=newEditText.OnKeyListener(){
@Override
publicbooleanonKey(Viewarg0,intarg1,KeyEventarg2){
//action跳轉,需要在AndroidManifest.xml中配置action
Intenti=newIntent("android.intent.action.mydialog");
MyEditText.this.startActivity(i);
returnfalse;
}
};
}
復制代碼Xml代碼<?xmlversion="1.0"encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.edit_text"android:versionCode="1"
android:versionName="1.0">
<applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
<activityandroid:name=".MyEditText"android:label="@string/app_name">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>
<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!--配置跳轉activity-->
<activityandroid:name="com.android.dialog.MyDialog">
<intent-filter>
<!--配置action路徑-->
<actionandroid:name="android.intent.action.mydialog"/>
<categoryandroid:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
<uses-sdkandroid:minSdkVersion="7"/></manifest>
⑤ 就是android 頁面的跳轉和傳值的問題,我想點一下button從第一個頁面跳到第三個頁面。但是我想把值
你思路就不對,你把第一個Activity里的值傳到第三個Activity 然後跳轉到第二個Activity 你也不能處理第三個Activity里的值啊,其實方法多了,可以傳到第二個Activity裡面 ,什麼時候跳轉到第三個Activity裡面的時候再傳給它
或者可以用sharepreference直接用文件的形式存儲,需要的時候不管在哪個Activity里直接拿出來就能用、
⑥ 安卓的跳轉和傳值能做什麼簡單的app
需要跳轉的時候
NSString *urlString = [NSString stringWithFormat:@"AppJumpSecond://%@",textField.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
我這里將textField的文字也傳過去
同樣的,在第二個頁面也是如此
NSString *urlString = [NSString stringWithFormat:@"AppJumpFirst://%@",textField.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
這樣就能相互跳轉了
4.處理傳過去的數據
在上面傳了textField的數據,接收時在AppDelegate的
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation方法里。
在AppDelegate里設置屬性
@property (nonatomic, strong) RootViewController *rvc;
在didFinishLaunchingWithOptions方法里添加
self.rvc = [[RootViewController alloc] init];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:self.rvc];
self.window.rootViewController = nc;
添加代碼塊
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
self.rvc.textField.text = [[url host] :NSUTF8StringEncoding];
return YES;
}
使得textField顯示另一個頁面傳過來的數據。