2

计算的.java:(这必须命令显示计算的.xml布局)

public class Calculated extends Activity {


   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.calculated);

   }
}

老:

主类:

 package com.barth.appie;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.EditText;


public class MainActivity extends Activity {


Button button1;
String text;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    addListenerOnButton();
}

public void addListenerOnButton() {

    button1 = (Button) findViewById(R.id.buttoncalculate);
    button1.setOnClickListener(new View.OnClickListener() {

    public void onClick(View view) {

        EditText editText = (EditText)findViewById(R.id.editText1);
        String text = editText.getText().toString();

             Intent myIntent = new Intent(view.getContext(),    Calculated.class);
             startActivityForResult(myIntent, 0);

        }
    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}



}

计算的.xml:(这是按下按钮后必须显示的内容)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="42dp"
    android:text="@string/text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

此外,我有<string name="text">Value</string>strings.xml,在另一个名为calculated.class的类中,我有它,以便它输出计算的.xml(工作正常)

我想要什么:我想在计算的.xml 中显示文本,这是一个在 main.class 中制作的字符串,我希望该字符串是填充在名为“editText1”的文本字段中的文本

4

2 回答 2

3
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.EditText;


public class MainActivity extends Activity {

Button button1;
String text;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    addListenerOnButton();
}

public void addListenerOnButton() {

    button1 = (Button) findViewById(R.id.buttoncalculate);
    button1.setOnClickListener(new View.OnClickListener() {

    public void onClick(View view) {

        EditText editText = (EditText)findViewById(R.id.editText1);
        String text = editText.getText().toString();

             Intent myIntent = new Intent(view.getContext(),Calculated.class);
             myIntent.putExtra("mytext",text);
             startActivity(myIntent);

        }
    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
  }
}

计算出来的.java

public class Calculated extends Activity {

TextView mTextview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.calculated);

       mTextview = (TextView)findViewById(R.id.textView1);

       mTextview.setText(getIntent().getStringExtra("mytext"));
}

计算的.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="42dp"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>
于 2013-02-24T13:12:52.937 回答
3

将数据从一个活动发送到另一个活动

MainActivity.java 是您要将数据发送到另一个活动的第一个活动。

Intent myIntent = new Intent(view.getContext(), Calculated.class);
myIntent.putExtra("text", text);
startActivity(myIntent);

Calculated.java 是第二个活动,它接收您从 MainActivity.java 传递的任何意图数据

String text = myIntent.getStringExtra("text");
TextView textView = (TextView)findViewById(R.id.textView1);
textView.setText(text);
于 2013-02-24T13:10:15.550 回答