- Forum-Beiträge: 16
02.04.2014, 20:50:22 via Website
02.04.2014 20:50:22 via Website
MainActivity.java
2
3import android.content.Intent;
4import android.os.Bundle;
5import android.support.v4.app.Fragment;
6import android.support.v7.app.ActionBarActivity;
7import android.view.LayoutInflater;
8import android.view.Menu;
9import android.view.MenuItem;
10import android.view.View;
11import android.view.ViewGroup;
12import android.widget.EditText;
13
14public class MainActivity extends ActionBarActivity {
15
16 public final static String EXTRA_MSG = "com.example.myfirstapp.MESSAGE";
17
18 @Override
19 protected void onCreate(Bundle savedInstanceState) {
20 super.onCreate(savedInstanceState);
21 setContentView(R.layout.activity_main);
22
23 if (savedInstanceState == null) {
24 getSupportFragmentManager().beginTransaction()
25 .add(R.id.container, new PlaceholderFragment())
26 .commit();
27 }
28 }
29
30 public void sendMessage(View view) {
31 Intent intent = new Intent(this, DisplayMessage.class);
32 EditText editText = (EditText) findViewById(R.id.editText1);
33 String message = editText.getText().toString();
34 intent.putExtra(EXTRA_MSG, message);
35 startActivity(intent);
36 }
37
38
39 @Override
40 public boolean onCreateOptionsMenu(Menu menu) {
41
42 // Inflate the menu; this adds items to the action bar if it is present.
43 getMenuInflater().inflate(R.menu.main, menu);
44 return true;
45 }
46
47 @Override
48 public boolean onOptionsItemSelected(MenuItem item) {
49 // Handle action bar item clicks here. The action bar will
50 // automatically handle clicks on the Home/Up button, so long
51 // as you specify a parent activity in AndroidManifest.xml.
52 int id = item.getItemId();
53 if (id == R.id.action_settings) {
54 return true;
55 }
56 return super.onOptionsItemSelected(item);
57 }
58
59 /**
60 * A placeholder fragment containing a simple view.
61 */
62 public static class PlaceholderFragment extends Fragment {
63
64 public PlaceholderFragment() {
65 }
66
67 @Override
68 public View onCreateView(LayoutInflater inflater, ViewGroup container,
69 Bundle savedInstanceState) {
70 View rootView = inflater.inflate(R.layout.fragment_main, container, false);
71 return rootView;
72 }
73 }
74
75}
activity_main.xml
2 xmlns:tools="XXX"
3 android:id="@+id/container"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent"
6 tools:context="com.example.myfirstapp.MainActivity"
7 tools:ignore="MergeRootFrame" >
8
9
10 <LinearLayout
11 android:layout_width="match_parent"
12 android:layout_height="match_parent"
13 android:orientation="horizontal" >
14
15 <EditText
16 android:id="@+id/editText1"
17 android:layout_weight="1"
18 android:layout_width="0dp"
19 android:layout_height="wrap_content"
20 android:hint="@string/your_text" >
21
22 </EditText>
23
24 <Button
25 android:id="@+id/button1"
26 android:layout_width="wrap_content"
27 android:layout_height="wrap_content"
28 android:text="@string/button_send"
29 android:onClick="sendMessage" />
30
31 </LinearLayout>
32
33</FrameLayout>
DisplayMessage.java
2
3import android.support.v7.app.ActionBarActivity;
4import android.support.v7.app.ActionBar;
5import android.support.v4.app.Fragment;
6import android.content.Intent;
7import android.os.Bundle;
8import android.view.LayoutInflater;
9import android.view.Menu;
10import android.view.MenuItem;
11import android.view.View;
12import android.view.ViewGroup;
13import android.widget.TextView;
14import android.os.Build;
15
16public class DisplayMessage extends ActionBarActivity {
17
18 @Override
19 protected void onCreate(Bundle savedInstanceState) {
20 super.onCreate(savedInstanceState);
21 setContentView(R.layout.fragment_display_message);
22
23 Intent intent = getIntent();
24 String message = intent.getStringExtra(MainActivity.EXTRA_MSG);
25
26 TextView textView = new TextView(this);
27 textView.setTextSize(40);
28 textView.setText(message);
29
30 setContentView(textView);
31
32 if (savedInstanceState == null) {
33 getSupportFragmentManager().beginTransaction()
34 .add(R.id.container, new PlaceholderFragment()).commit();
35 }
36 }
37
38 @Override
39 public boolean onCreateOptionsMenu(Menu menu) {
40
41 // Inflate the menu; this adds items to the action bar if it is present.
42 getMenuInflater().inflate(R.menu.display_message, menu);
43 return true;
44 }
45
46 @Override
47 public boolean onOptionsItemSelected(MenuItem item) {
48 // Handle action bar item clicks here. The action bar will
49 // automatically handle clicks on the Home/Up button, so long
50 // as you specify a parent activity in AndroidManifest.xml.
51 int id = item.getItemId();
52 if (id == R.id.action_settings) {
53 return true;
54 }
55 return super.onOptionsItemSelected(item);
56 }
57
58 /**
59 * A placeholder fragment containing a simple view.
60 */
61 public static class PlaceholderFragment extends Fragment {
62
63 public PlaceholderFragment() {
64 }
65
66 @Override
67 public View onCreateView(LayoutInflater inflater, ViewGroup container,
68 Bundle savedInstanceState) {
69 View rootView = inflater.inflate(R.layout.fragment_display_message,
70 container, false);
71 return rootView;
72 }
73 }
74
75}
Manifest
2<manifest xmlns:android="XXX"
3 package="com.example.myfirstapp"
4 android:versionCode="1"
5 android:versionName="1.0" >
6
7 <uses-sdk
8 android:minSdkVersion="8"
9 android:targetSdkVersion="19" />
10
11 <application
12 android:allowBackup="true"
13 android:icon="@drawable/ic_launcher"
14 android:label="@string/app_name"
15 android:theme="@style/AppTheme" >
16 <activity
17 android:name="com.example.myfirstapp.MainActivity"
18 android:label="@string/app_name" >
19 <intent-filter>
20 <action android:name="android.intent.action.MAIN" />
21
22 <category android:name="android.intent.category.LAUNCHER" />
23 </intent-filter>
24 </activity>
25 <activity
26 android:name="com.example.myfirstapp.DisplayMessage"
27 android:label="@string/title_activity_display_message"
28 android:parentActivityName="com.example.myfirstapp.MainActivity" >
29 <meta-data
30 android:name="android.support.PARENT_ACTIVITY"
31 android:value="com.example.myfirstapp.MainActivity" />
32 </activity>
33 </application>
34
35</manifest>
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.