我无法从单个卡片视图 Activity 导航到多个活动。我可以导航到只有 1
I cannot navigate to multiple activities from a single Card View Activity. I can navigate to Only 1
我是第一次使用cardview。我创建了 6 张卡片并让它们听用户点击,但只有第一个卡片视图有效并导航到另一个工作的下一个 activity、none。连点击方法我都试过了
我已经尝试通过 XML 使用 OnClickListner 和 onClick。在 Java 中,它没有给出任何错误,但我无法导航,在 XML 中,当我编写 onClick 方法时,它只是给出错误 "cannot find....." 类似的东西。
<?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 的相应 activity。
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);
}
});
}
}
可能你应该在 XML 中为所有卡片定义 onClick 属性,然后在你的 activity 中你应该定义你在 XML 中给出的名称的方法,然后你应该根据被点击的卡片id写一个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属性中,这样每当点击卡片时,它都会调用相应activity[中的myMethod()]
我是第一次使用cardview。我创建了 6 张卡片并让它们听用户点击,但只有第一个卡片视图有效并导航到另一个工作的下一个 activity、none。连点击方法我都试过了
我已经尝试通过 XML 使用 OnClickListner 和 onClick。在 Java 中,它没有给出任何错误,但我无法导航,在 XML 中,当我编写 onClick 方法时,它只是给出错误 "cannot find....." 类似的东西。
<?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 的相应 activity。
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);
}
});
}
}
可能你应该在 XML 中为所有卡片定义 onClick 属性,然后在你的 activity 中你应该定义你在 XML 中给出的名称的方法,然后你应该根据被点击的卡片id写一个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属性中,这样每当点击卡片时,它都会调用相应activity[中的myMethod()]