简体   繁体   中英

Can't make the button refer to another activity in android

I want the button to show some texts when it is clicked. When we click the 'formula' button, a new blank page should open up in the phone showing the layout of the activity.. Here are my codes

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

FormulaActivity.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 coding)

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

You just need to intialize the Button and EditText inside your MainActivity.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);
}

Your

public final static String EXTRA_MESSAGE = "";
is empty string. Try changing it to any valued string.

Hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM