我能够将字符串从Java活动传递给一类librarie,但我无法确定如何发送多个,我需要传递三个,这里是代码:
OFActivity.java
package cc.openframeworks.androidMultiOFActivitiesExample;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import cc.openframeworks.OFAndroid;
import cc.openframeworks.androidMultiOFActivitiesExample.R.id;
public class OFActivityA extends cc.openframeworks.OFActivity{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
OFAndroid of = new OFAndroid("jala");
}
OFAndroid.java
public class OFAndroid {
private static String text ;
public OFAndroid(String text) {
this.text = text ;
}
public void useText() {
Log.e("TAG" , this.text);
}
答案 0 :(得分:3)
尝试使用a)数组,b)列表或c)捆绑包
OFActivity.java
public class OFActivityA extends cc.openframeworks.OFActivity{
// onCreate()
OFAndroid of = new OFAndroid(new String[]{"jala", "foo", "bar"});
}
OFAndroid.java
public class OFAndroid {
private String[] textArray ;
public OFAndroid(String[] textArray) {
this.textArray= textArray;
}
public void useText() {
for (String text : textArray) {
Log.e("TAG" , text);
}
}
OFActivity.java
public class OFActivityA extends cc.openframeworks.OFActivity{
// onCreate()
List<String> listOfString = new ArrayList<>();
listOfString.add("jala");
listOfString.add("foo");
listOfString.add("bar");
OFAndroid of = new OFAndroid(listOfString);
}
OFAndroid.java
public class OFAndroid {
private List<String> listOfString;
public OFAndroid(List<String> listOfString) {
this.listOfString = listOfString;
}
public void useText() {
for (String text : listOfString) {
Log.e("TAG" , text);
}
}
OFActivity.java
public class OFActivityA extends cc.openframeworks.OFActivity{
// onCreate()
Bundle bundle = new Bundle();
bundle.putString("keyOfString1", "jala");
bundle.putString("keyOfString2", "foo");
bundle.putString("keyOfString3", "bar");
OFAndroid of = new OFAndroid(bundle);
}
OFAndroid.java
public class OFAndroid {
private Bundle bundle ;
public OFAndroid(Bundle bundle) {
this.bundle = bundle;
}
public void useText() {
Log.e("TAG", bundle.getString("keyOfString1")); // jala
Log.e("TAG", bundle.getString("keyOfString2")); // foo
Log.e("TAG", bundle.getString("keyOfString3")); // bar
}
顺便说一句,在OFAndroid.java中是否有任何使用静态变量的理由? 如果不仔细控制生命周期和静态对象的引用,很容易导致内存泄漏问题。