2017-11-22 4 views
-1

Ich bin neu bei Android und versuche ein kleines Projekt zu machen. Und für die Erstellung der Homepage habe ich Android CardView verwendet. here is the homepage view link Ich weiß nicht, wie man von der Hauptaktivität zur zweiten Aktivität wechselt. Also, bitte helfen Sie mir beim Schreiben des Java-Teils.Android CardView bewegt sich von einer Aktivität zu einer anderen Aktivität. Wie?

Angenommen, ich klicke auf die Monatskartenansicht, ich muss zu einer anderen Aktivität gehen. wie es geht? Mein MainActivity.xml Code ist wie folgt:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.example.shubhojit.atry.MainActivity" 
android:orientation="vertical" 
android:padding="10dp" 
android:background="@color/backgroundcolor" 
android:gravity="center"> 

<LinearLayout 
android:clipToPadding="false" 
android:gravity="center" 
android:orientation="horizontal" 
android:layout_width="match_parent" 
android:layout_height="wrap_content"> 
<android.support.v7.widget.CardView 
    android:foreground="?android:attr/selectableItemBackground" 
    android:clickable="true" 
    android:id="@+id/mon" 
    android:layout_width="160dp" 
    android:layout_height="190dp" 
    android:layout_margin="10dp"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:gravity="center"> 
     <ImageView 
      android:layout_width="64dp" 
      android:layout_height="64dp" 
      android:background="@drawable/cerclebackgroundpink" 
      android:src="@drawable/ic_date_range_black_24dp" 
      android:padding="10dp"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textStyle="bold" 
      android:layout_marginTop="10dp" 
      android:text="MONTHS"/> 
     <View 
      android:layout_width="match_parent" 
      android:layout_height="1dp" 
      android:background="@color/lightgray" 
      android:layout_margin="10dp"/> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:text="Find festivals of your favourite Month" 
      android:padding="5dp" 
      android:textColor="@android:color/darker_gray"/> 
    </LinearLayout> 
</android.support.v7.widget.CardView> 

<android.support.v7.widget.CardView 
    android:foreground="?android:attr/selectableItemBackground" 
    android:clickable="true" 


    android:layout_width="160dp" 
    android:layout_height="190dp" 
    android:layout_margin="10dp"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:gravity="center"> 
     <ImageView 
      android:layout_width="64dp" 
      android:layout_height="64dp" 
      android:background="@drawable/cerclebackgroundpurple" 
      android:src="@drawable/ic_terrain_black_24dp" 
      android:padding="10dp"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textStyle="bold" 
      android:layout_marginTop="10dp" 
      android:text="PLACES"/> 
     <View 
      android:layout_width="match_parent" 
      android:layout_height="1dp" 
      android:background="@color/lightgray" 
      android:layout_margin="10dp"/> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:text="Check out festivals in your Region" 
      android:padding="5dp" 
      android:textColor="@android:color/darker_gray"/> 
    </LinearLayout> 
</android.support.v7.widget.CardView> 
</LinearLayout> 


<LinearLayout 
    android:clipToPadding="false" 
    android:gravity="center" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <android.support.v7.widget.CardView 
     android:foreground="?android:attr/selectableItemBackground" 
     android:clickable="true" 

     android:layout_width="340dp" 
     android:layout_height="150dp" 
     android:layout_margin="10dp"> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      android:gravity="center"> 
      <ImageView 
       android:layout_width="64dp" 
       android:layout_height="64dp" 
       android:background="@drawable/cerclebackgroundgreen" 
       android:src="@drawable/ic_people_black_24dp" 
       android:padding="10dp"/> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textStyle="bold" 
       android:layout_marginTop="10dp" 
       android:text="CULTURES"/> 
      <View 
       android:layout_width="match_parent" 
       android:layout_height="1dp" 
       android:background="@color/lightgray" 
       android:layout_margin="10dp"/> 
      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:gravity="center" 
       android:text="Live out with festivals of your own Culture" 
       android:padding="5dp" 
       android:textColor="@android:color/darker_gray"/> 
     </LinearLayout> 
     </android.support.v7.widget.CardView> 

    </LinearLayout> 
    </LinearLayout> 

Antwort

0

Zunächst einmal müssen Sie ID

<ImageView 
android:id="@+id/myImage" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
/> 

für die Elemente auf Ihrer MainActivity.xml Datei zuweisen, weil Sie diese ID benötigen eine Verbindung zu Ihrer Java-Klasse zu machen.

Sobald dies erledigt ist, gehen Sie zu Ihrer Java-Klasse und verdrahten sie in die Elemente in Ihrer Java-Klasse. Stellen Sie sicher, dass die Elemente übereinstimmen, da Sie kein Image in Java mit einer TextView in XML verbinden können. Hier kann zum Beispiel ein Bild nehmen (Code geht in onCreate Methode): -

ImageView myImage = (ImageView)findViewById(R.id.myImage); 

Jetzt eine OnClickListener schaffen, die für die Ausführung von Code verantwortlich ist, wenn Sie auf myImage Elemente klicken.

myImage.setOnClickListener(new OnClickListener){ 
Intent intent = new Intent(MainActivity.this, AnotherActivity.class); 
startActivity(intent); 
} 

MainActivity.this ist das Element, das Ihre aktuelle Tätigkeit beschreiben und AnotherActivity.class ist das Element, das die Aktivität, die Sie navigieren wollen beschreiben.

Bitte beachten Sie, dies ist kostenlos handley geschrieben, wie ich Android Studio auf meinem Laptop derzeit nicht habe so arbeiten entsprechend der Syntax in Android Studio zur Verfügung gestellt. Lassen Sie es mich wissen, wenn Sie weitere Erläuterungen benötigen.

0

Haben Sie einen Code versucht dann hier posten Sie Ihre Aktivitätsklasse.

+0

Dies sollte ein Kommentar sein (beantwortet die Frage nicht). –

+0

Aber ich kann nicht kommentieren, weil mein Ruf 49 @BrettJeffreson ist – Deepa

Verwandte Themen