当前位置:首页 » 安卓系统 » androidactivity传值

androidactivity传值

发布时间: 2022-09-08 23:01:28

㈠ 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到普通类

什么叫普通类?在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中传值

什么叫普通类?在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怎么向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的方法。

㈤ android activity之间传值

不要随便使用static
虽然简单但是很容易造成内存泄漏等问题。可以使用handle传值,每一次主activity更新了值就发msg出来,或者用回调,或者广播。

㈥ 【Android 开发】 : Activity之间传递数据的几种方式

1. 使用Intent来传递数据Intent表示意图,很多时候我们都会利用Android的Intent来在各个Activity之间传递数据,这也是Android比较官方的一种数据传递的方式需求1:从一个Activity(IntentDemo)跳转到另外一个Activity(Other),其中利用Intent来传递数据程序Demo如下:IntentDemo.javapackage com.android.intentdemo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class IntentDemo extends Activity { private Button button; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initComponent(); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(IntentDemo.this, Other.class); // 在Intent中传递数据 intent.putExtra("name", "AHuier"); intent.putExtra("age", 22); intent.putExtra("address", "XiaMen"); // 启动Intent startActivity(intent); } }); } private void initComponent() { button = (Button) findViewById(R.id.button); } } other.javapackage com.android.intentdemo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class Other extends Activity { private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.other); initComponent(); Intent intent = getIntent(); int age = intent.getIntExtra("age", 0); String name = intent.getStringExtra("name"); String address = intent.getStringExtra("address"); textView.setText("My age is " + age + "\n" + "My name is " + name + "\n" + "My address " + address); } private void initComponent() { textView = (TextView) findViewById(R.id.msg); } } ——>2. 在Activity之间使用静态变量传递数据在上例中使用Intent可以很方便的在不同的Activity之间传递数据,这个也是官方推荐的方式,但是也有一定的局限性,就是Intent无法传递不能序列化的对象。我们可以使用静态变量来解决这个问题。需求1:从一个Activity(IntentDemo)跳转到另外一个Activity(Other),其中利用静态变量来传递数据程序Demo:IntentDemo.java Intent intent = new Intent(); intent.setClass(IntentDemo.this, Other.class); Other.age = 22; Other.name = "AHuier"; Other.address = "XiaMen"; startActivity(intent); Other.java private TextView textView; public static int age; public static String name; public static String address; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.other); initComponent(); textView.setText("My age is " + age + "\n" + "My name is " + name + "\n" + "My address " + address); }——>3. 通剪切板传递数据 在Activity之间数据传递还可以利用一些技巧,不管是Windows还是Linux操作系统,都会支持一种剪切板的技术,也就是一个程序将一些数据复制到剪切板上,然后其他的任何程序都可以从剪切板中获取数据。1) 利用剪切板传递普通的数据,如字符串 需求1:从一个Activity(IntentDemo)跳转到另外一个Activity(Other),通过剪切板传递数据 程序Demo:IntentDemo.java ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); String name = "AHuier"; clipboardManager.setText(name); Intent intent = new Intent(IntentDemo.this, Other.class); startActivity(intent);Other.javaClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); String msgString = clipboardManager.getText().toString(); textView.setText(msgString);——> 1) 利用剪切板传递复杂的数据,如对象需求1:从一个Activity(IntentDemo)跳转到另外一个Activity(Other),通过剪切板传递数据新建一个MyData.javapackage com.android.intentdemo; import java.io.Serializable; public class MyData implements Serializable { private String name; private int age; public MyData(String name, int age) { super(); this.name = name; this.age = age; } // 提供一个toString()方法 @Override public String toString() { return "MyData [name=" + name + ", age=" + age + "]"; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } IntentDemo.java MyData myData = new MyData("AHuier", 22); //将对象转换成字符串 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); String base64String = ""; try { ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(myData); //使用Android中提供的 Base64 工具类,这个类主要是用来对对象进行压缩也解码的过程,使用默认方式 base64String = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT); objectOutputStream.close(); } catch (Exception e) { // TODO: handle exception } ClipboardManager clipboardManager = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE); clipboardManager.setText(base64String); Intent intent = new Intent(IntentDemo.this, Other.class); startActivity(intent); Other.java ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); String msgString = clipboardManager.getText().toString(); //将字符串 msgString 还原为对象 byte[] base64_byte = Base64.decode(msgString, Base64.DEFAULT); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(base64_byte); try { ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream); MyData myData = (MyData)objectInputStream.readObject(); textView.setText(myData.toString()); } catch (Exception e) { // TODO: handle exception }——>4.Intent中使用全局变量来传递数据需求1:从一个Activity(Main)跳转到另外一个Activity(Other),通过全局变量来传递数据Main.javaprivate Button button; private MyApp myApp; ... myApp = (MyApp)getApplication(); myApp.setName("kunhuixu"); //修改之后的名称 Intent intent = new Intent(Main.this, Other.class); startActivity(intent);Other.javaprivate MyApp myApp; private TextView textView; ... myApp = (MyApp)getApplication(); textView.setText("--- The app name ---" + myApp.getName());MyApp.javapackage com.android.intentglobal; import android.app.Application; /* * 查看Android官方文档。 * Application 是所有那些需要维护全局application状态的基类。你可以提供你自己的实现机制通过在在AndroidManifest.xml中提供你自己的需要声明 * 的标记你自己的标签。 * onCreate()方法是在应用程序启动的时候被回调的。 ——>

㈦ 如何在Android开发中activity之间数据传递

android各组件之间可以使用Intent来传递数据, 这里以ActivityA向ActivityB传递数据为例

Intentintent=newIntent(context,ActivityB.class);

//如果context上下文不是Activity的话,需要添加下面这个flag,
if(!(contextinstanceofActivity)){
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
//添加要传递的数据
//这里的key到时候ActivityB会用来得到value
//android要求可以传递的数据类型包括基本数据类型,
//基本数据类型的数组,CharSequence,以及实现了Parcelable的对象
//putExtra是重载方法,根据value的类型会适当的打包进intent的Bundle对象里
intent.putExtra(key,value);
//或则将数据打包进Bundledata里,然后intent.putExtras(data);

context.startActivity(intent);

在ActivityB中

publicvoidonCreate(BundlesavedInstanceState){
//得到传递过来的数据
Bundlebundle=getIntent().getExtras();
//通过getXXX方法来获取key对应的value
bundle.getXXX(key);
}

㈧ android activity怎么给fragment传值

参数传递方法一

在Activity中定义一个字段、然后添加set和get方法、代码如下、mTitle就是要传递的参数、如果是传递对象、可以把mTitle换成一个对象即可

㈨ android中activity A需要传值给C,但是A是跳转到B的。

在a里用startactivityforresult启动b,退出b后a能接受到b的值,跳转c前把b传过来的值用intent也一起传过去,或者不关闭a,也是用startactivityforresult启动c,c关闭后值传回来到a里

热点内容
中国移动用什么服务密码 发布:2024-05-20 00:52:10 浏览:695
make编译输出 发布:2024-05-20 00:37:01 浏览:67
4200存储服务器 发布:2024-05-20 00:20:35 浏览:160
解压小生活 发布:2024-05-20 00:15:03 浏览:143
粘土小游戏服务器ip 发布:2024-05-20 00:14:00 浏览:196
魔兽世界如何快速增加服务器 发布:2024-05-19 23:53:37 浏览:694
安卓手机如何转入苹果手机内 发布:2024-05-19 23:50:35 浏览:405
安卓哪个能安装血染小镇 发布:2024-05-19 23:45:57 浏览:901
tensorflowmac编译 发布:2024-05-19 23:28:59 浏览:702
sqlmaxvarchar 发布:2024-05-19 23:24:02 浏览:703