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

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
    android:id="@+id/formula"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="sendMessage"
    android:text="Formula" />
4

2 に答える 2

1

あなたはただあなたの内部を初期化する必要がButtonありEditTextます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);
}
于 2013-01-04T13:20:30.083 に答える
0

あなたの

public final static String EXTRA_MESSAGE = "";
空の文字列です。任意の値の文字列に変更してみてください。

これがお役に立てば幸いです。

于 2013-01-04T13:21:21.987 に答える