制作一个按钮以显示祝酒并将数据发送到另一个活动

时间:2016-04-15 10:22:55

标签: android android-studio android-activity

我正在尝试将数据从一个活动发送到另一个活动,并在按下按钮时显示吐司我正在尝试同时完成两个。我分配的按钮的ID是add_to_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" />

我知道有很多新手的错误,对不起那个

3 个答案:

答案 0 :(得分:1)

从其他活动中获取数据:

Intent intent = new Intent(currentactivity.this, targetactivity.class);
                intent.putExtra("XXX", value);

你需要一个String值,在这种情况下:text和一个字符串名称,在这种情况下:uid

然后在另一项活动中获取uid:

String uid = getIntent().getExtras().getString("XXX");

答案 1 :(得分:1)

所以,假设您已经打开了应该收到Activity之前的intent。这里的问题是,如果你离开这个activity而不是finish它,只要系统不需要太多资源,它仍然存在。那么,您将转到发送意图的下一个activity,然后再次开始接收activity。但这就是诀窍:activity刚刚回到屏幕,但从未死过。因此未收到intent。这样做吧

发送intent

 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);
        }
    });
   }

离开后在intentactivity接收finish

    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();   
         }         
    }

从更新的问题来看,我认为您理解activities lifecycle的顺序存在一般性问题。 OnCreate()将在activity开始时调用views并设置String variables。您设置为textView的{​​{1}}不会自动更改,您必须在button click method中执行此操作。因此,如果您不想直接在开始时将以上示例添加到onClick

@Override
public void onClick(View v){

 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);
  }
}

答案 2 :(得分:0)

在数据接收方面这样做....

    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);