我点了3 textviews
,点击其中任意一个,我想要#34; Text"将这些textviews
中的任何一个发送到另一个类。
这是textViews的XML
<TextView
android:id="@+id/brouni"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:gravity="left"
android:paddingTop="10dp"
android:text=" 1-I will help my brother go to university"
android:textSize="15dp" />
<TextView
android:id="@+id/goal2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingTop="10dp"
android:text=" 2-I will buy my mother a house"
android:textSize="15dp" />
<TextView
android:id="@+id/goal3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingTop="10dp"
android:text=" 3-I will finish this application"
android:textSize="15dp" />
</LinearLayout>
这是包含它们的java类
public class Family extends Activity implements OnClickListener {
TextView brouni1;
TextView goal2;
TextView goal3;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.family);
goal3 = (TextView) findViewById(R.id.goal3);
goal3.setOnClickListener(this);
goal2 = (TextView) findViewById(R.id.goal2);
goal2.setOnClickListener(this);
brouni1 = (TextView) findViewById(R.id.brouni);
brouni1.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.brouni:
Intent open = new Intent(Family.this, Xp.class);
brouni1.getText();
startActivity(open);
break;
case R.id.goal2:
Intent open1 = new Intent (Family.this , Xp.class);
startActivity(open1);
break;
case R.id.goal3:
Intent open11 = new Intent(Family.this , Xp.class);
startActivity(open11);
break;
}
}
}
这是我希望将textview
内的文本发送到
public class Xp extends Activity {
Button accept;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.xp);
accept = (Button) findViewById(R.id.accept);
accept.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(Xp.this, Drawer.class);
startActivity(i);
finish();
}
});
}
}
最后,这是我希望在选择&#34;接受&#34;之后出现相同文本的类(主类)。 。并保存在那里,可以删除,也可以在以后添加更多文本。
public class MainClass extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
我把它留空了,所以你们可以建议我使用正确的代码来恢复这些意图并保存它们。
总结:如何在另一个类中重现包含文本的意图,以及
如何将它们应用于sharedpreferences
以将它们保存在它们被发送到的类中?
答案 0 :(得分:0)
在Activity
之间传递数据时,请使用Intent
至putExtra
,如下所示
case R.id.goal3:
Intent open11 = new Intent(Family.this , Xp.class);
open11.putExtra("intent_extra", goal3.getText().toString());
startActivity(open11);
break;
当您接收Activity
时,您可以获取此值,最后将该值保存在SharedPreference
中。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("intent_extra");
SharedPreferences settings = getSharedPreferences("intent_pref", 0);
SharedPReferences.Editor edit = settings.edit();
edit.putString("intent_pref", value);
edit.apply();
}
}