我刚刚开始学习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;
}
}
}
view.setOnClickListener()
。 - Soheil