我想将一个onclicklistener应用到cardview里面的textview,没有RECYCLER VIEW。我的代码如下

时间:2016-04-06 02:38:15

标签: android onclicklistener android-cardview

xml layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">



    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:layout_scrollFlags="scroll|enterAlways">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingTop="24dp"
            android:background="@color/back"
            >

            <android.support.v7.widget.CardView
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:layout_margin="16dp"
                android:layout_marginBottom="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:layout_margin="16dp"
                    >
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Central Library"
                        android:textSize="18dp"
                        android:id="@+id/lib"/>


                </LinearLayout>

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

            <android.support.v7.widget.CardView
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:layout_margin="16dp"
                android:layout_marginBottom="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:layout_margin="16dp">
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Dispensary"
                        android:textSize="18dp"
                        android:id="@+id/dis"/>


                </LinearLayout>

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

            <android.support.v7.widget.CardView
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:layout_margin="16dp"
                android:layout_marginBottom="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:layout_margin="16dp">
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Auditorium"
                        android:textSize="18dp"
                        android:id="@+id/audi"/>


                </LinearLayout>

            </android.support.v7.widget.CardView>
            <android.support.v7.widget.CardView
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:layout_margin="16dp"
                android:layout_marginBottom="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:layout_margin="16dp">
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Bank and ATM"
                        android:textSize="18dp"
                        android:id="@+id/atm"/>


                </LinearLayout>

            </android.support.v7.widget.CardView>
            <android.support.v7.widget.CardView
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:layout_margin="16dp"
                android:layout_marginBottom="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:layout_margin="16dp">
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="VIP Guest House"
                        android:textSize="18dp"
                        android:id="@+id/gh"/>


                </LinearLayout>

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

        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>


</android.support.design.widget.CoordinatorLayout>

java code ..

package com.example.hp.manit;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

//code starts from here

public class Facilities extends AppCompatActivity implements View.OnClickListener{

    Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_facilities);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        TextView t1=(TextView)findViewById(R.id.lib);
        t1.setOnClickListener(this);
        TextView t2=(TextView)findViewById(R.id.dis);
        t2.setOnClickListener(this);
        TextView t3=(TextView)findViewById(R.id.audi);
        t3.setOnClickListener(this);
        TextView t4=(TextView)findViewById(R.id.atm);
        t4.setOnClickListener(this);
        TextView t5=(TextView)findViewById(R.id.gh);
        t5.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.lib:
                // do your code
                intent = new Intent(Facilities.this, Library.class);
                startActivity(intent);
                break;

            case R.id.dis:
                // do your code
                intent = new Intent(Facilities.this, Library.class);
                startActivity(intent);
                break;

            case R.id.audi:
                // do your code
                intent = new Intent(Facilities.this, Auditorium.class);
                startActivity(intent);
                break;
            case R.id.atm:
                // do your code
                intent = new Intent(Facilities.this, BankATM.class);
                startActivity(intent);
                break;
            case R.id.gh:
                // do your code
                intent = new Intent(Facilities.this, GuestHouse.class);
                startActivity(intent);
                break;
        }

    }
}

注意:此布局文件facilities.xml在选项卡布局中的片段中膨胀。

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Bundle args = getArguments();
    int tabPositon = args.getInt(TAB_POSITION);

    if(tabPositon==0) {
        View v0 = inflater.inflate(R.layout.activity_academics, container, false);

        return v0;
    }
    else if(tabPositon==1) {
        View v1 = inflater.inflate(R.layout.activity_departments, container, false);

        return v1;
    }
    else if(tabPositon==2) {
        View v2 = inflater.inflate(R.layout.activity_facilities, container, false);

        return v2;
    }
    else {

        View v3 = inflater.inflate(R.layout.activity_students_zone, container, false);

        return v3;
    }

}

我已经为每个textview应用了一个监听器,用于使用intent打开一个新活动。 但它不起作用

我只想打开一个点击cardview的新活动 Plz帮助怎么?????

1 个答案:

答案 0 :(得分:0)

只需在XML中的所有Textview中添加以下属性,然后尝试

    <machineKey 
    validationKey="AutoGenerate,IsolateApps"
    decryptionKey="AutoGenerate,IsolateApps"
    validation="HMACSHA256"
    decryption="Auto"
    />