1

我希望按钮在单击时显示一些文本。当我们单击“公式”按钮时,手机中应该会打开一个新的空白页面,显示活动的布局。这是我的代码

MainActivity.java

public class MainActivity extends Activity {
    public final static String EXTRA_MESSAGE = "";

    // ....

    public void sendMessage(View view) {
    Intent intent = new Intent(MainActivity.this, FormulaActivity.class);
    EditText editText = (EditText) findViewById(R.id.edit);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
     // Do something in response to button
    startActivity(intent);

公式活动.java

public class FormulaActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        // Set the text view as the activity layout
        setContentView(textView);
    }

MainActivity.xml(按钮编码)

<Button
    android:id="@+id/formula"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="sendMessage"
    android:text="Formula" />
4

2 回答 2

1

你只需要在你的内部初始化ButtonEditTextMainActivity.java

XML Layout File

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

<EditText 
    android:id="@+id/edit"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    />

   <Button 
    android:id="@+id/formula"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Formula"
    android:gravity="bottom"
    />

 public class MainActivity extends Activity
 {
   public void onCreate(Bundle savedInstanceState)
   {
     super.onCreate(savedInstanceState);
     Button F=(Button)findViewById(R.id.formula);
     EditText editText = (EditText) findViewById(R.id.edit);

      F.setOnClickListener(new View.onClickListener()
       {
          public void onClick()
          {
            sendMessage(); 
          }
       }

    }

    public void sendMessage() 
      {
      String message = editText.getText().toString();
      Intent intent = new Intent(MainActivity.this, FormulaActivity.class);
      intent.putExtra("EXTRA_MESSAGE", message);       
      startActivity(intent);
      }
  }

public class FormulaActivity extends Activity 
{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();
    String message = intent.getStringExtra("EXTRA_MESSAGE");

    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(message);

    // Set the text view as the activity layout
    setContentView(textView);
}
于 2013-01-04T13:20:30.083 回答
0

您的

public final static String EXTRA_MESSAGE = "";
是空字符串。尝试将其更改为任何有值的字符串。

希望这会帮助你。

于 2013-01-04T13:21:21.987 回答