android传值
‘壹’ 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);