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