Я не могу перейти к нескольким действиям из одного представления карты.Я могу перейти только 1 - PullRequest
0 голосов
/ 16 февраля 2019

Я впервые использую cardview.Я создал 6 карточек и заставил их слушать щелчок пользователя, но работает только первая карточка, переходящая к следующему действию, никакая другая работа.Даже я попробовал все методы Click.

Я пытался использовать OnClickListner и onClick через XML.В Java это не дает никакой ошибки, но я не могу ориентироваться, в XML, когда я пишу метод onClick, он просто выдает ошибку «не могу найти .....» что-то в этом роде.

     <?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=".landing_screen"
    android:background="@drawable/bg"
    android:orientation="vertical"
    android:padding="5dp"
    android:gravity="center"
    >
    <TextView
       android:layout_marginBottom="0dp"
        android:textSize="20sp"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Please Choose Who you are"/>
    <LinearLayout

        android:clipToPadding="false"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <android.support.v7.widget.CardView
        app:cardBackgroundColor="#"
        android:onClick="passenger"
        android:foreground="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:layout_width="140dp"
        android:layout_height="140dp"
        android:layout_margin="5dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:gravity="center">

            <ImageView
                android:padding="5dp"
                android:src="@drawable/passenger"
                android:layout_width="56dp"
                android:layout_height="64dp"
                android:background="@drawable/circlebgpurple"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:layout_marginTop="2dp"
                android:text="Passenger"
                android:textColor="#000"

                />
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@color/line"
                android:layout_margin="2dp"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="I am a Passenger"
                android:textColor="#000"
                android:padding="5dp"
                />

        </LinearLayout>
    </android.support.v7.widget.CardView>
        <android.support.v7.widget.CardView
            app:cardBackgroundColor="#"
            android:id="@+id/relativecard"
            android:foreground="?android:attr/selectableItemBackground"
            android:clickable="true"
            android:layout_width="140dp"
            android:layout_height="140dp"
            android:layout_margin="5dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:gravity="center">

                <ImageView
                    android:padding="5dp"
                    android:src="@drawable/boy"
                    android:layout_width="56dp"
                    android:layout_height="64dp"
                    android:background="@drawable/circlebggreen"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textStyle="bold"
                    android:layout_marginTop="2dp"
                    android:text="Relative"
                    android:textColor="#000"

                    />
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/line"
                    android:layout_margin="2dp"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="I am a Relative"
                    android:textColor="#000"
                    android:padding="5dp"
                    />
            </LinearLayout>
        </android.support.v7.widget.CardView>
    </LinearLayout>
    <LinearLayout
        android:clipToPadding="false"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <android.support.v7.widget.CardView
            app:cardBackgroundColor="#"
            android:id="@+id/drivercard"
            android:foreground="?android:attr/selectableItemBackground"
            android:clickable="true"
            android:layout_width="140dp"
            android:layout_height="140dp"
            android:layout_margin="5dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:gravity="center">

                <ImageView
                    android:padding="8dp"
                    android:src="@drawable/driver"
                    android:layout_width="56dp"
                    android:layout_height="64dp"
                    android:background="@drawable/circlebgyellow"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textStyle="bold"
                    android:layout_marginTop="2dp"
                    android:text="Driver"
                    android:textColor="#000"

                    />
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/line"
                    android:layout_margin="2dp"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="I am a Driver"
                    android:textColor="#000"
                    android:padding="5dp"
                    />

            </LinearLayout>
        </android.support.v7.widget.CardView>
        <android.support.v7.widget.CardView
            app:cardBackgroundColor="#"
            android:id="@+id/tkchecker"
            android:foreground="?android:attr/selectableItemBackground"
            android:clickable="true"
            android:layout_width="140dp"
            android:layout_height="140dp"
            android:layout_margin="5dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:gravity="center">

                <ImageView
                    android:padding="5dp"
                    android:src="@drawable/ic_ticket_checker"
                    android:layout_width="56dp"
                    android:layout_height="64dp"
                    android:background="@drawable/circlebgpink"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textStyle="bold"
                    android:layout_marginTop="2dp"
                    android:text="Ticket Checker"
                    android:textColor="#000"

                    />
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/line"
                    android:layout_margin="2dp"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="I'm a TicketChecker"
                    android:textColor="#000"
                    android:padding="5dp"
                    />

            </LinearLayout>
        </android.support.v7.widget.CardView>



    </LinearLayout>
    <LinearLayout
        android:clipToPadding="false"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <android.support.v7.widget.CardView
            app:cardBackgroundColor="#"
            android:id="@+id/subadmin"
            android:foreground="?android:attr/selectableItemBackground"
            android:clickable="true"
            android:layout_width="140dp"
            android:layout_height="140dp"
            android:layout_margin="5dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:gravity="center">

                <ImageView
                    android:padding="5dp"
                    android:src="@drawable/subadmin"
                    android:layout_width="56dp"
                    android:layout_height="64dp"
                    android:background="@drawable/circlebgyellow"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textStyle="bold"
                    android:layout_marginTop="2dp"
                    android:text="Sub-Admin"
                    android:textColor="#000"

                    />
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/line"
                    android:layout_margin="2dp"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="I am a Sub-Admin"
                    android:textColor="#000"
                    android:padding="5dp"
                    />

            </LinearLayout>
        </android.support.v7.widget.CardView>
        <android.support.v7.widget.CardView
            app:cardBackgroundColor="#"
            android:id="@+id/admin"
            android:foreground="?android:attr/selectableItemBackground"
            android:clickable="true"
            android:layout_width="140dp"
            android:layout_height="140dp"
            android:layout_margin="5dp">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:gravity="center">

                <ImageView
                    android:layout_width="56dp"
                    android:layout_height="64dp"
                    android:background="@drawable/circlebggreen"
                    android:padding="5dp"
                    android:src="@drawable/manager" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textStyle="bold"
                    android:layout_marginTop="5dp"
                    android:text="Admin"
                    android:textColor="#000"

                    />
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/line"
                    android:layout_margin="2dp"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="I am the Admin"
                    android:textColor="#000"
                    android:padding="2dp"
                    />

            </LinearLayout>
        </android.support.v7.widget.CardView>



    </LinearLayout>

</LinearLayout>


[![enter image description here][1]][1]

Я просто хочуперейдите к соответствующему действию каждого CardView при нажатии.

Java-код:

public class relative extends AppCompatActivity {

    CardView relativecard;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sign_up);
        relativecard = (CardView)findViewById(R.id.relativecard);


        relativecard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent relative = new Intent(getApplicationContext(), signup.class);
                startActivity(relative);
            }
        });

    }

}

1 Ответ

0 голосов
/ 16 февраля 2019

Вероятно, вы должны определить атрибут onClick в XML для всех карточек, а затем в своей деятельности вы должны определить метод, имя которого вы дали в XML, а затем вы должны написать оператор if-else или switch на основе идентификатора.карты, по которой щелкают, а затем по любому виду, по которому вы щелкаете, необходимо сопоставить и выполнить действие.Примерно так:

public void myMethod(View v){
     switch(v.getId()){
          case R.id.cardOne:
               //do something here when card one is clicked
          break;
          case R.id.cardTwo:
              //do something here when card two is clicked.
          break;
          .
          . and so on.
 }

В вашем XML-файле вам нужно поместить myMethod в атрибут onClick каждой карты, чтобы при каждом нажатии карты она вызывала myMethod () в соответствующем действии

...