Android Development - Snippets: Unterschied zwischen den Versionen
Aus Wikizone
| Zeile 77: | Zeile 77: | ||
main_activtiy.xml | main_activtiy.xml | ||
<pre> | <pre> | ||
| + | <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
| + | xmlns:tools="http://schemas.android.com/tools" | ||
| + | android:layout_width="match_parent" | ||
| + | android:layout_height="match_parent" | ||
| + | tools:context=".MainActivity" > | ||
| + | |||
| + | <Button | ||
| + | android:id="@+id/button1" | ||
| + | android:layout_width="wrap_content" | ||
| + | android:layout_height="wrap_content" | ||
| + | android:layout_alignParentTop="true" | ||
| + | android:layout_centerHorizontal="true" | ||
| + | android:text="@string/button" /> | ||
| + | |||
| + | </RelativeLayout> | ||
</pre> | </pre> | ||
Version vom 28. Februar 2013, 15:17 Uhr
Neue Activity starten
Z.B. über Button Klick
Du brauchst:
- Neue Activity
- Evtl. neues Layout anlegen
- Neue Java Klasse für Activity anlegen
- Neue Activity im Manifest eintragen
- Button in der Haupt Activity
- onClick Event Handler
- Intent als Verbindung
- Ressourcen wie Strings etc.
Neues Layout für die Activity
Layout Ordner > Rechtsklick > New > Android Layout File > Name in Lowercase(z.b. page_2)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/page2_title"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Neue Activity Klasse erzeugen
Package > Rechtsklick > New > Class > Name mit FirstLetterUppercase (z.B. Page2Activity)
- extends Activity
- Activity importieren
- mind. onCreate(bundle) -> am Besten über Rechtsklick > Source > Override
- setcontentView(R.layout.id)
page2Activity.class
package com.example.buttonstartsactivity;
import android.app.Activity;
import android.os.Bundle;
public class Page2Activity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page2);
}
}
Manifest Eintrag
// ...
<activity
android:name="com.example.buttonstartsactivity.Page2Activity"
android:label="@string/app_name"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.PAGE2" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
// ...
Button in Hauptansicht
z.B. main_activtiy.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/button" />
</RelativeLayout>