Android Studio: Start another activity: Difference between revisions
From OnnoCenterWiki
Jump to navigationJump to search
Created page with "==Respond to the Send button== file app > java > com.example.myfirstapp > MainActivity, add the following sendMessage() method stub: public class MainActivity extends AppCo..." |
No edit summary |
||
| Line 15: | Line 15: | ||
} | } | ||
} | } | ||
Klik View > Alt+Enter > Import class | |||
file app > res > layout > activity_main.xml | |||
* klik button > Attribute > onClick property > sendMessage [MainActivity] | |||
==Build an intent== | |||
Tambahkan di MainActivity | |||
public class MainActivity extends AppCompatActivity { | |||
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE"; | |||
@Override | |||
protected void onCreate(Bundle savedInstanceState) { | |||
super.onCreate(savedInstanceState); | |||
setContentView(R.layout.activity_main); | |||
} | |||
/** Called when the user taps the Send button */ | |||
public void sendMessage(View view) { | |||
Intent intent = new Intent(this, DisplayMessageActivity.class); | |||
EditText editText = (EditText) findViewById(R.id.editTextTextPersonName); | |||
String message = editText.getText().toString(); | |||
intent.putExtra(EXTRA_MESSAGE, message); | |||
startActivity(intent); | |||
} | |||
} | |||
Revision as of 00:45, 4 March 2022
Respond to the Send button
file app > java > com.example.myfirstapp > MainActivity, add the following sendMessage() method stub:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user taps the Send button */
public void sendMessage(View view) {
// Do something in response to button
}
}
Klik View > Alt+Enter > Import class
file app > res > layout > activity_main.xml
- klik button > Attribute > onClick property > sendMessage [MainActivity]
Build an intent
Tambahkan di MainActivity
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user taps the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editTextTextPersonName);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}