當前位置:首頁 » 安卓系統 » android傳值

android傳值

發布時間: 2022-02-16 23:32:44

『壹』 android開發,請問下兩個Activity之間如何傳遞textView上的值

在startActivity的會後,通過Intent中傳值,比如

java">//啟動B
Intentbintent=newIntent(A.this,B.class);
//設置bintent的Bundle的一個值
Stringbsay=textView.getText().toString();//獲取textview的值
bintent.putExtra("listenB",bsay)
startActivity(bintent,0);

『貳』 android中Activity類和BroadcastReceiver類之間如何傳值

android中組件傳值可以使用Intent類,裡面有一個bundle類,用於保存數據,可以在activity或者BroadcastReceiver類中得到,示例如下:

啟動一個Activity,可以使用如下方法
Intentintent=newIntent(this,BroadcastClass);
intent.putExtra(name,value);
sendBroadcast(intent);

『叄』 Android,兩個Fragment之間怎麼用Argument傳值

Fragment之間的傳值交互無法直接進行,也不建議直接進行。需要通過activity進行中轉。

簡單的例子

1、Activity定義
publicclassActextendsActivity{
privateFragmentf1=null;
privateFragmentf2=null;
publicinterfaceMyCallBack{//定義回調介面
voidcallBack(Stringparam);//回調方法
}
publicvoidonCreate(Bundlebd){
super.onCreate(bd);
f1=(Fragment)findViewById(R.id.xxxx1);//獲取fragment1
f2=(Fragment)findViewById(R.id.xxxx2);//獲取fragment2
f1.setCallBack(newMyCallBack(){//往fragment1中設置回調介面,便於傳遞參數到activity中
publicvoidcallBack(Stringparam){
f2.showParam(param);//回調介面中,把參數傳遞到fragment2中
}
});
}
}
2、Fragment1定義
{
MyCallBackcb=null;
publicvoidsetCallBack(MyCallBackcb){//設置回調介面
this.cb=cb;
}
publicvoidpostParam(){
this.cb.callBack("2222");//調用回調介面
}
}

3、Fragment2定義
{
publicvoidshowParam(Stringparams){//顯示輸入的值
...顯示params
}
}

『肆』 android里 activity怎麼向service傳遞參數

android中activity中向service傳遞參數,有如下方法:

1.在Activity里注冊一個BroadcastReceiver,Service完成某個任務就可以發一個廣播,接收器收到廣播後通知activity做相應的操作。
2.使用bindService來關聯Service和Application,應用.apk里的所有組件一般情況都運行在同一個進程中,所以不需要用到IPC,bindService成功後,Service的Client可以得到Service返回的一個iBinder引用,具體的參見Service的文檔及onBind的例子,這樣Service的引用就可以通過返回的iBinder對象得到,如
public class LocalService extends Service {
// This is the object that receives interactions from clients. See
// RemoteService for a more complete example.
private final IBinder mBinder = new LocalBinder();

public class LocalBinder extends Binder {
LocalService getService() {
return LocalService.this;
}
}

@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
}

之後Client通過這個iBinder對象得到Service對象引用之後,可以直接和Service通訊,比如讀取Service中的值或是調用Service的方法。

『伍』 java web項目怎麼向android項目傳值

可使用Android自帶的httpClient實現Android與java web之間的數據的交互。
具體實現代碼:
1. GET 方式傳遞參數
//先將參數放入List,再對參數進行URL編碼
List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();
params.add(new BasicNameValuePair("param1", "數據")); //增加參數1
params.add(new BasicNameValuePair("param2", "value2"));//增加參數2
String param = URLEncodedUtils.format(params, "UTF-8");//對參數編碼
String baseUrl = "伺服器介面完整URL";
HttpGet getMethod = new HttpGet(baseUrl + "?" + param);//將URL與參數拼接
HttpClient httpClient = new DefaultHttpClient();
try {
HttpResponse response = httpClient.execute(getMethod); //發起GET請求
Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //獲取響應碼
Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8"));//獲取伺服器響應內容
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

2. POST方式 方式傳遞參數
//和GET方式一樣,先將參數放入List
params = new LinkedList<BasicNameValuePair>();
params.add(new BasicNameValuePair("param1", "Post方法"));//增加參數1
params.add(new BasicNameValuePair("param2", "第二個參數"));//增加參數2
try {
HttpPost postMethod = new HttpPost(baseUrl);//創建一個post請求
postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8")); //將參數填入POST Entity中
HttpResponse response = httpClient.execute(postMethod); //執行POST方法
Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //獲取響應碼
Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8")); //獲取響應內容
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

『陸』 Android 普通類中怎麼向activity中傳值

什麼叫普通類?在android四大組件當中,傳值普遍採用的是Intent還有Bundle這樣的形式
以Text為例
發送方是 Intent intent = new Intent(Context context,otherActivityOrService.class);
intent.putExtra("key","yourText");

startActivity(intent);//如果啟動的是service就使用startService(intent);

接收方如果是Service或是BroadcastReceiver會在生命周期方法中直接獲得intent
如果接收方仍然是一個Activity那麼應該使用Intent intent = getIntent();
然後用
String text = intent.getStringExtra("key"); 這樣的方式來獲取傳過來的值

『柒』 android activity之間傳值

不要隨便使用static
雖然簡單但是很容易造成內存泄漏等問題。可以使用handle傳值,每一次主activity更新了值就發msg出來,或者用回調,或者廣播。

『捌』 android中一個頁面如何同時傳值到多個頁面

1、最簡單的方法,設置一個public static變數,直接更改這個值,其他界面可以直接使用,不過不是很推薦。
2、使用廣播發送消息,其他界面接收。

『玖』 android開發如何在頁面之間傳參

第一個頁面跳轉 傳遞值
Button bn1=(Button)findViewById(R.id.btn_Login); //跳轉
bn1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Intent intent=new Intent(tiaoz.this,nexts.class);
//傳值
EditText txt_username=(EditText)findViewById(R.id.edit_username);
EditText txt_password=(EditText)findViewById(R.id.edit_password);

Bundle bundle = new Bundle();
bundle.putString("key_username", txt_username.getText().toString());
bundle.putString("key_password", txt_password.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
finish();
}
});
第二個頁面接收值
Bundle bunde = this.getIntent().getExtras();
String strs="用戶名:"+bunde.getString("key_username").toString()+"密碼:"+bunde.getString("key_password").toString();
//改變文本框的文本內容
show.setText(strs);

熱點內容
反編譯添加自啟管理 發布:2025-03-15 01:39:30 瀏覽:428
駕校上傳學時 發布:2025-03-15 01:24:30 瀏覽:14
如何給密碼加密 發布:2025-03-15 01:23:03 瀏覽:840
php加密原理 發布:2025-03-15 01:08:46 瀏覽:461
計算機分級存儲體系 發布:2025-03-15 01:07:59 瀏覽:362
上古卷軸解壓 發布:2025-03-15 00:54:25 瀏覽:383
uniqlinux 發布:2025-03-15 00:48:37 瀏覽:467
易燃壓縮瓶 發布:2025-03-15 00:42:55 瀏覽:451
網頁上傳圖片不顯示 發布:2025-03-15 00:23:56 瀏覽:363
存儲晶元的片選 發布:2025-03-15 00:14:26 瀏覽:715