2017-11-22 5 views
-1

私はアンドロイドには新しく、小さなプロジェクトを作ろうとしています。ホームページを作るために私はアンドロイドCardViewを使用しています。 here is the homepage view link メインアクティビティから第2アクティビティへの移行方法はわかりません。だから、plsはJavaの部分を書いて私を助けます。Android CardViewはあるアクティビティから別のアクティビティに移動します。どうやって?

月のカードビューをクリックすると、別のアクティビティに移動する必要があります。どうやってするの? マイMainActivity.xmlコードは以下の通りです:

<?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> 

答えて

0

まず第一に、あなたはあなたのJavaクラスへの接続を行うためにこれらのIDが必要になりますので、IDをあなたのMainActivity.xmlファイル上の要素のための

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

を割り当てる必要があります。

終了したら、Javaクラスに移動し、Javaクラスの要素に配線します。 JavaのImageをXMLのTextViewに接続することができないため、要素が一致していることを確認してください。 (コードのonCreateメソッドに行く)、例えば、画像を撮ることができます: -

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

は今、あなたはMYIMAGE要素をクリックしてコードを実行する責任があるOnClickListenerを作成します。

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

MainActivity.thisは、あなたの現在の活動を説明要素であり、AnotherActivity.classはあなたにナビゲートしたい活動を説明要素です。

私はノートパソコンにアンドロイドスタジオを持っていないので、これは無料ハンドリーとして書かれていますので、Androidスタジオ内で提供されている構文に従って動作してください。それ以上の説明が必要な場合はお知らせください。

0

あなたがここにあなたの活動のクラスを投稿し、その後任意のコードを試してみました。

+0

これはコメントだったはずです(質問には答えません)。 –

+0

しかし私の評判は49 @BrettJeffresonですからコメントを書いてはいけません – Deepa

関連する問題