如何向CardView添加单击侦听器?

时间:2015-03-24 20:45:52

标签: java android xml

我是Android开发的新手,对于一般的编码还是比较新的,我正试图让它当我点击其中一张卡时,它会显示一个吐司。每张卡不同的一张。我到处都看,没有任何作用。

这是我的一张卡片。我的所有卡都包含在RelativeLayout中      

    <FrameLayout
        android:id="@+id/frame_orange"
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/ripple"
        android:foreground="?android:attr/selectableItemBackground">

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="118dp"
            android:layout_height="123dp"
            android:layout_gravity="center"
            android:scaleType="fitXY"
            android:src="@drawable/pi" />

    </FrameLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal|bottom"
        android:paddingBottom="10dp"
        android:text="Definition"
        android:textColor="@android:color/black"
        android:textSize="25dp" />
</android.support.v7.widget.CardView>

这是我添加到MainActivity.java

的内容
public static class PlaceHolderFragment extends Fragment implements View.OnClickListener{

View rootView;

public PlaceHolderFragment(){

}

public View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.activity_main, container, false);
    return rootView;
}

@Override
public void onClick(View v) {
    switch (v.getId())
    {
        case R.id.card_definition:
            Toast.makeText(getActivity().getApplication(), "Test",
                    Toast.LENGTH_LONG).show();
    }
}
}

我不确定如果我这样做完全错了,但我会很感激帮助。

编辑:我的整个MainActivity.java

package com.piplex.kylefoster.piplex;


import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;


public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}


public static class PlaceHolderFragment extends Fragment {

    View rootView;

    public PlaceHolderFragment() {

    }

    public View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.activity_main, container, false);
        CardView cardView = (CardView) findViewById(R.id.card_definition);

        cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity().getApplication(), "Test",
                        Toast.LENGTH_LONG).show();
            }
        });
        return rootView;

    }
}
}

1 个答案:

答案 0 :(得分:3)

您可以轻松地将此代码添加到public View OnCreateView

    CardView cardView = (CardView) findViewById(R.id.card_view);

    cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //write your function here.
        }
    });