2012-06-07 5 views
14

Wie kann ich eine andere XML-Layoutdatei öffnen, wenn ich auf eine Schaltfläche in der main.xml-Datei klicke?Wie öffne ich das Layout beim Klicken auf die Schaltfläche (Android)

also, wenn ich main.xml, die eine Schaltfläche sying hat klicken Sie hier und ich klicke es, es öffnet sich second.xml Datei (Layout).

+0

Bitte sagen Sie uns, was Sie versucht, zuerst gestartet werden soll. –

+0

Sie müssen zwei Aktivitäten verwenden, um zwischen zwei Layouts zu wechseln. Jede Aktivität behandelt ihr eigenes Layout, das ist der beste Weg, dies zu tun. –

Antwort

25

Diese tutorial erklären Sie genau, was Sie Schritt für Schritt benötigen.

Zuerst erstellen Sie Ihre zwei Layout:

main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#ffffff" > 

    <TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="#000000" 
    android:text="This is Activity 1" /> 

     <Button android:text="Next" 
     android:id="@+id/Button01" 
     android:layout_width="250px" 
      android:textSize="18px" 
     android:layout_height="55px"> 
    </Button>  

</LinearLayout> 

Second.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#ffffff" > 

    <TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="#000000" 
    android:text="This is Activity 2" /> 

     <Button android:text="Previous" 
     android:id="@+id/Button02" 
     android:layout_width="250px" 
      android:textSize="18px" 
     android:layout_height="55px"> 
    </Button>  

</LinearLayout> 

Zweite Fügen Sie Ihre Aktivität auf der Manifest-Datei

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="com.rr" 
     android:versionCode="1" 
     android:versionName="1.0"> 
    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity android:name=".Activity1" 
        android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".Activity2"></activity> 
    </application> 
    <uses-sdk android:minSdkVersion="3" /> 
</manifest> 

Activity1.java

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

public class Activity1 extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Button next = (Button) findViewById(R.id.Button01); 
     next.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       Intent myIntent = new Intent(view.getContext(), Activity2.class); 
       startActivityForResult(myIntent, 0); 
      } 

     }); 
    } 
} 

zu Activity2 wechseln müssen Sie:

  1. auf die Schaltfläche, um eine Referenz Rufen mit ID Button01 auf dem Layout mit (Button) findViewById(R.id.Button01).

  2. Erstellen Sie einen OnClick-Listener für die Schaltfläche.

  3. Und der wichtigste Teil, erstellt eine "Absicht", eine andere Aktivität zu starten. Die Absicht benötigt zwei Parameter: einen Kontext und der Name die Aktivität, die wir (Activity2.class)

Activity2.java

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

public class Activity2 extends Activity { 

    /** Called when the activity is first created. */ 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.second); 

     Button next = (Button) findViewById(R.id.Button02); 
     next.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       Intent intent = new Intent(); 
       setResult(RESULT_OK, intent); 
       finish(); 
      } 

     }); 
    } 
2

-Inflate die Schaltfläche aus dem XML-
einem OnClickListener -add auf sie
-set ein neues Layout in dem OnClick-Ereignisse

Button btn = (Button) findViewById(R.id.myButton); 
btn.setOnClickListener(new OnClickListener(){ 

@Override 
public void onClick(View v) 
{ 
    MyActivity.setContentView(R.layout.newlayout); 
} 

}); 

So etwas wie dies funktionieren soll ...

+0

GREAT! das funktioniert –

+0

wie mache ich 'öffentliche void onClick() { MyActivity.setContentView (R.layout.newlayout); } in XML –

+0

das ist afaik nicht möglich, die beste Sache, die Sie tun könnten, ist, den onClick Listener in XML zu definieren, das "android: onClick" verwendet. Die Logik davon muss im Java-Code sein. – Thkru

Verwandte Themen