Android 制作按钮以显示祝酒词并将数据发送到其他活动
我正在尝试将数据从一个活动发送到另一个活动,并在按下按钮时显示toast。我正在尝试同时完成这两个操作。我分配的按钮id为Android 制作按钮以显示祝酒词并将数据发送到其他活动,android,android-studio,android-activity,Android,Android Studio,Android Activity,我正在尝试将数据从一个活动发送到另一个活动,并在按下按钮时显示toast。我正在尝试同时完成这两个操作。我分配的按钮id为add\u to\u cart,我在下面尝试了此代码,但没有工作 Button customToastButton = (Button) this.findViewById(R.id.add_to_cart); customToastButton.setOnClickListener(new View.OnClickListener() { publ
add\u to\u cart
,我在下面尝试了此代码,但没有工作
Button customToastButton = (Button) this.findViewById(R.id.add_to_cart);
customToastButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//get the LayoutInflater and inflate the custom_toast layout
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.cart_toast, (ViewGroup)
findViewById(R.id.toast_layout_root));
//get the TextView from the custom_toast layout
TextView text = (TextView) layout.findViewById(R.id.toastText);
text.setText("Item as been added to cart");
//create the toast object, set display duration,
//set the view as layout that's inflated above and then call show()
Toast t = new Toast(getApplicationContext());
t.setDuration(Toast.LENGTH_SHORT);
t.setView(layout);
t.show();
Bundle intent = getIntent().getExtras();
if (intent != null) {
firstname= intent.getString("Gladwin");
secondname= intent.getString("james");
}
}
});
toast部分工作正常,但不是数据发送部分,我尝试单独实现此代码来发送数据,但没有响应,下面是我用来接收发送数据的代码
private String firstname;
private String secondname;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
TextView txtFirst=(TextView)findViewById(R.id.text1);
txtFirst.setText(firstname);
TextView txtSecond=(TextView)findViewById(R.id.textview1);
txtSecond.setText(secondname);
这是接收端的XML部分
<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:text=""
android:textSize="20sp" />
<TextView
android:layout_below="@+id/text1"
android:id="@+id/textview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:text=""
android:textSize="20sp" />
我知道有很多新手犯的错误,很抱歉数据接收端的人会这样做
private String firstname;
private String secondname;
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
firstname= bundle.getString("firstname");
secondname= bundle.getString("Secondname");
}
TextView txtFirst=(TextView)findViewById(R.id.text1);
txtFirst.setText(firstname);
TextView txtSecond=(TextView)findViewById(R.id.textview1);
txtSecond.setText(secondname);
要从其他活动获取数据,请执行以下操作:
Intent intent = new Intent(currentactivity.this, targetactivity.class);
intent.putExtra("XXX", value);
您需要一个字符串值,在本例中为:text,在本例中为:uid
然后,要在另一个活动中获取uid:
String uid = getIntent().getExtras().getString("XXX");
因此,假设您已经打开了
活动
,该活动应该在之前收到意图
。这里的问题是,如果您离开这个活动
而不完成它,那么只要系统不需要太多资源,它仍然是活动的。因此,您将进入下一个活动
,在那里发送意图并再次开始接收活动
。但诀窍是:活动刚刚回到屏幕上,但从未消失。因此,未接收到意图。这样做
发送意图
:
public class SendingActivity extends Activity {
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
Intent testIntent = new Intent(SendingActivity.this, ReceivingActivity.class);
testIntent.putExtra("firstname","Eggsy");
testIntent.putExtra("lastname","Iggsy");
startActivity(testIntent);
}
});
}
public class ReceivingActivity extends Activity {
//make TextView objects
private TextView txtFirst;
private TextView txtSecond;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activty_xml);
//initialize textViews <-- this has to be done with all views
txtFirst = (TextView)findViewById(R.id.yourFirstTextViewId);
txtSecond = (TextView)findViewById(R.id.yourSecondTextViewId);
Intent intent = getIntent();
String firstName = intent.getStringExtra("firstname");
if(firstName!=null){
txtFirst.setText(firstName);
}
String lastName = intent.getStringExtra("lastname");
if(lastName!=null){
txtSecond.setText(lastName);
}
}
@Override
onBackPressed(){
finish();
super.onBackPressed();
}
}
在活动中接收意图
,并在离开后完成:
public class SendingActivity extends Activity {
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
Intent testIntent = new Intent(SendingActivity.this, ReceivingActivity.class);
testIntent.putExtra("firstname","Eggsy");
testIntent.putExtra("lastname","Iggsy");
startActivity(testIntent);
}
});
}
public class ReceivingActivity extends Activity {
//make TextView objects
private TextView txtFirst;
private TextView txtSecond;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activty_xml);
//initialize textViews <-- this has to be done with all views
txtFirst = (TextView)findViewById(R.id.yourFirstTextViewId);
txtSecond = (TextView)findViewById(R.id.yourSecondTextViewId);
Intent intent = getIntent();
String firstName = intent.getStringExtra("firstname");
if(firstName!=null){
txtFirst.setText(firstName);
}
String lastName = intent.getStringExtra("lastname");
if(lastName!=null){
txtSecond.setText(lastName);
}
}
@Override
onBackPressed(){
finish();
super.onBackPressed();
}
}
@Eggsy在将值分配给字符串之前检查bundle是一种更安全的方法…如何在第二个活动中调用上面的代码我提供的代码就足够了吗???现在我被打印成这样“firstname”“secondname”它实际上没有获取实际值name@Eggsy代码是正确的…所以,您必须调试您的应用程序或在I上应用日志在传递意图之前您是否打开了接收意图的活动?是的,我只能抛出这样的代码“public void add_to_cart(View View){intent nextentint=new intent(this,cart.class);startActivity(nextentint);}ok,活动可能仍处于打开状态,无法获得目的。如果是这样的话,要么在离开时必须完成此接收活动,要么以其他方式重写受保护的方法onNewIntent()并在那里捕获它。但是你必须将清单中的属性singleTop
设置为该活动。我是新手,我听不懂你说的话,请你说清楚一点或者编辑代码好吗,所以你说接收活动,我们称之为ActivityB,是以前打开的。现在,例如按下后退按钮,您将离开该活动。但它在系统中仍然存在。这里应该做的是重写onBackPressed()并在内部调用finish()来完成此活动。然后,如果你再次启动它,每次都应该传递意图…@Eggsy,因为这是我的代码。让我编辑代码。它经过编辑,您必须将XXX更改为您想要的任何内容,然后value是您想要转换到其他活动的值。希望您不会介意我问为什么txtFirst和txtSecond的亮度很高,并说s无法解析符号您必须在活动中创建对象,请参阅ReceiveActivity中的我的编辑。此文本查看您必须使用布局xml的id引用的视图。我非常感谢你,我在一天的时间里从你那里学到了很多,我非常感谢你的耐心和一直以来对我的支持。再次感谢你,不客气,我在这里也得到了很多支持。这是给予和索取的一部分:)是否可以通过删除startActivity(testIntent)发送数据而不启动我尝试的活动;但是仍然没有工作,你能帮我做些什么吗。。???