Android Intent信息传递

时间:2013-06-09 02:01:30

标签: android xml android-intent android-activity

我是Android新手,一直在寻找答案,但似乎无法解决我的问题。我的意图是将信息从一个活动传递到另一个活动。当setContentView()设置为TextView时,活动接收将显示它。如果我尝试将我的xml文件与setContentView()一起使用,它将不会显示字符串。

意图

public void clockin (View view){
    Intent intent = new Intent (getApplicationContext(), Mainmenu.class);
    String timedate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance90.getTime()0;
    intent.putExtra("TIME_DATE", timedate);
    startActivity(intent);

活动

public class Mainmenu extends Activity{
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        Bundle extras = getIntent().getExtras();
        if(extras!=null){
        String timedate = extras.getString("TIME_DATE");

        TextView textView = new TextView (this);
        text.View.setTextSize(20);
        text.View.setText(timedate);
        setContentView(R.layout.activity_mainmenu);}}

xml

<TextView
     android:id="@+id/TIME_DATE"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:textSize="20sp"
     />

4 个答案:

答案 0 :(得分:2)

Android惯用法,用于命名可以防止出现问题的额外内容:

   Intent intent = new Intent (getApplicationContext(), Mainmenu.class);
   intent.putExtra(Mainmenu.EXTRA_DATE, timedate)

的活动:

    public class Mainmenu extends Activity {

      public final static String EXTRA_DATE="TIME_DATE":      

      protected void onCreate(Bundle savedInstanceState){

          ....              

          String timedate = extras.getString(EXTRA_DATE);

答案 1 :(得分:1)

你的MainMenu应该是

 public class Mainmenu extends Activity{
 protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    // setContentView(R.layout.activity_mainmenu);  
    Bundle extras = getIntent().getExtras();
    if(extras!=null){
    String timedate = extras.getString("Time_Date");// key must match
    TextView textView = new TextView (this); // initialize textview
    textView.setTextSize(20);
    textView.setText(timedate);
    setContentView(textView);   // set textview as layout content 
    }
    }

              or

activity_mainmenu.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textSize="20sp" 
        android:text="TextView" />

</RelativeLayout>

的MainMenu

     public class Mainmenu extends Activity{
     protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mainmenu);  
    Bundle extras = getIntent().getExtras();
    if(extras!=null){
    String timedate = extras.getString("Time_Date");
    TextView textView = (TextView) findViewById(R.id.textView1);
    textView.setText(timedate); 
    }
    }

答案 2 :(得分:0)

额外的名称区分大小写,即:

String timedate = extras.getString("Time_Date");

答案 3 :(得分:0)

在你的第二项活动中,试试这个:

Intent intent = getIntent();
String timedate = intent.getStringExtra("Time_Date");