Android Development - Snippets: Unterschied zwischen den Versionen

Aus Wikizone
Wechseln zu: Navigation, Suche
(Der Seiteninhalt wurde durch einen anderen Text ersetzt: „== Activities == === Android - Neue Activity anlegen und starten === Android - Neue Activity Lock Portrait or Lanscape Click Liste…“)
Zeile 1: Zeile 1:
 +
== Activities ==
 +
=== Android - Neue Activity anlegen und starten ===
 +
[[Android - Neue Activity]]
 
Lock Portrait or Lanscape
 
Lock Portrait or Lanscape
  
Zeile 5: Zeile 8:
  
 
Bitmap - ressource und input stream
 
Bitmap - ressource und input stream
 
 
 
 
 
== 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)
 
<pre>
 
<?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>
 
</pre>
 
 
=== 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'''
 
<pre>
 
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);
 
}
 
}
 
</pre>
 
 
=== Manifest Eintrag ===
 
<pre>
 
// ...
 
        <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>
 
// ...
 
</pre>
 
 
=== Button in Hauptansicht erstellen ===
 
z.B.
 
 
'''main_activtiy.xml'''
 
<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>
 
 
=== Button verbinden ===
 
 
'''MainActivity.java'''
 
<pre>
 
package de.webmynet.buttonstartsactivity;
 
 
import android.app.Activity;
 
import android.content.Intent;
 
import android.os.Bundle;
 
import android.view.Menu;
 
import android.view.View;
 
import android.widget.Button;
 
 
public class MainActivity extends Activity {
 
 
@Override
 
protected void onCreate(Bundle savedInstanceState) {
 
super.onCreate(savedInstanceState);
 
setContentView(R.layout.activity_main);
 
Button button1 = (Button) findViewById(R.id.button1);
 
button1.setOnClickListener(new View.OnClickListener() {
 
@Override
 
public void onClick(View v) {
 
//btw v is the view where the button is pressed - you can use it to retrieve data
 
startActivity(new Intent("de.webmynet.buttonstartsactivity.PAGE2"));
 
}
 
});
 
}
 
 
@Override
 
public boolean onCreateOptionsMenu(Menu menu) {
 
// Inflate the menu; this adds items to the action bar if it is present.
 
getMenuInflater().inflate(R.menu.activity_main, menu);
 
return true;
 
}
 
 
}
 
 
</pre>
 
 
=== Variante onClick Event im Layout File ===
 
Anstatt der onClick Funktion kann man auch eine Funktion im Layout definieren. Dann kann man in der .java Datei einfach die Funktion implementieren.
 
activity_main.xml
 
<pre>
 
 
...
 
<Button
 
    android:layout_width="wrap_content"
 
    android:layout_height="wrap_content"
 
    android:text="@string/button"
 
    android:onClick="buttonPressed" />
 
...
 
</pre>
 
MainActivity.java
 
<pre>
 
/** Called when the user clicks the send button **/
 
public void buttonPressed(View view) {
 
Intent intent = new Intent(this, Page2Activity.class);
 
EditText editText = (EditText) findViewById(R.id.button);
 
startActivity(intent);
 
}
 
</pre>
 

Version vom 2. März 2013, 08:11 Uhr

Activities

Android - Neue Activity anlegen und starten

Android - Neue Activity Lock Portrait or Lanscape

Click Listeners auf view object fullscreen - requestwindowfeature

Bitmap - ressource und input stream