Android Activity <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Examples/examples + ILXju/examples + oaM8bGk + zO7C + examples/LV38rH1NrKudPDw/zB7tDQuaS + 36 OsxMfDtNTaPGNvZGU + release/CvM/release + release/qOsyOe5 + release/WoaM8bGk + zazR + release/release + wP3W0LK7u + release/zJvrX0oaM8bGk + release/release + VXA8L2VtPiDKwrz + o6yxo7PWy/release + release/ydbY08O1xMSjv + mho7j8tuC52NPaRnJhZ21lbnS1xNDFz6KjrMfrsum/release "note"> Tip:If you are not using the latest ADT plug-in, your activity may look different. Make sure that you have installed the latest ADT plugin to complete this course. DisplayMessageActivity
The class should now be like this:
public class DisplayMessageActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_display_message); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()).commit(); } } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } /** * A placeholder fragment containing a simple view. */ public static class PlaceholderFragment extends Fragment { public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_display_message, container, false); return rootView; } }}
If the IDE you are using is not EclipseDisplayMessageActivity
Class to the above Code.
AllActivity
Must be implementedonCreate()
The system will call this method when creating a new activity instance. This method is requiredsetContentView()
Method to specify the layout of the activity, and also the place where you want to initialize the activity component.
Tip:If you use a non-Eclipse IDE, your project does not containactivity_display_message
Layout file. It doesn't matter. You will update this method immediately and do not need to use this layout file.
Add a title string If you use Eclipse, you can adjust it to the next section (next section), because the Template already provides the title string.
If you are using another IDE, add the title of the new activitystrings.xml
File:
...
My Message
Add a new Activity to the Manifest File All activities must be declared in the manifest file,AndroidManifest.xml
, Use
Element.
When you are using Eclipse, it creates a default object. If you are not using Eclipse, you need to add the list entity by yourself. As shown below:
...
android:parentActivityName
Declares the name of the parent activity of the activity in the application logic level. The system will implement default navigation Behaviors Based on this value, such as Up navigation in Android4.1 or later. You can also use the Support Library package, and then
Add this value to the element.
Tip:Your Android SDK should have the latest Support package. It is included in the ADT Bundle, but if you are not using Eclipse, you need to install it in the Adding Platforms and Packages step. When you use an Eclipse template, the Support package is automatically added to your project. If you are not using Eclipse, You need to manually add this Library Island to your project and follow the setting up the Support Library operation to return here.
If you use Eclipse for development, you can run the program now, but nothing will happen. When the Send button is clicked, it will jump to the new activity, but it uses the default "Hello World" interface. You will update the activity immediately and use a custom text view to display it. If you are not using Eclipse and cannot compile it, don't worry.
Receive Intent EachActivity
Will beIntent
Call, whether it is a user operation or not. You can callgetIntent()
Method to obtain the intent that enables your activity, or the data contained in it.
InDisplayMessageActivity
ClassonCreate()
Method, get throughMainActivity
Passed information:
Intent intent = getIntent();String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
Display Information To display the information on the screen, you must createTextView
Control, and then usesetText()
Method. Then addTextView
As the Root View of the activity layout.
CompleteonCreate()
The method should be as follows:
@Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get the message from the intent Intent intent = getIntent(); String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); // Create the text view TextView textView = new TextView(this); textView.setTextSize(40); textView.setText(message); // Set the text view as the activity layout setContentView(textView);}
Now you can run your application. After it is started, enter the information in the text box and click send. The entered information will appear in the second activity.
Figure 2. Two activities in the app run on android 4.4
You have created your first application!