- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试通过单击不同的 CardView 从我的应用程序调用号码,但它对我不起作用。
每次我尝试添加 Intent 时,startActivity 都会变成红色并弹出错误消息“无法解析方法”。我什至尝试在 startActivity() 之前添加上下文,但它会使应用程序崩溃。
我有 4 个 CardViews 可以点击以发起调用和我有 3 个 Java 类:声明 recyclerView Holder 的 CardViewDataAdapter、MainActivity 和 itemObject在 activity_main 中我使用了 recycler_view。在 cardview_row 中,我使用了 card_view 并将其与回收站 View 绑定(bind)。
请帮助我,我用谷歌搜索了这个问题,也在堆栈溢出上查找了它,但还没有找到解决方案。
CardViewDataAdapter.java代码:
public class CardViewDataAdapter extends RecyclerView.Adapter<CardViewDataAdapter.ViewHolder> {
private List<ItemObject> itemList;
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
// each data item is a string and an image in this case
public ImageView image_view;
public TextView image_name;
private Context context;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
this.image_view = (ImageView) itemLayoutView.findViewById(R.id.image_view);
this.image_name = (TextView) itemLayoutView.findViewById(R.id.image_name);
// Store the context
this.context = context;
// Attach a click listener to the entire row view
itemLayoutView.setOnClickListener(this);
}
// Handles the row being being clicked
@Override
public void onClick(View view) {
if (getPosition() == 0){
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:123456789"));
startActivity(callIntent);
} else if (getPosition() == 1){
Toast.makeText(view.getContext(), "position2 = " + getPosition(), Toast.LENGTH_SHORT).show();
} else if (getPosition() == 2){
Toast.makeText(view.getContext(), "position3 = " + getPosition(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(view.getContext(), "position4 = " + getPosition(), Toast.LENGTH_SHORT).show();
}
//context.startActivity(intent);
}
}
// Create new views (invoked by the layout manager)
@Override
public CardViewDataAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.cardview_row, null);
// create ViewHolder
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
// - get data from your itemsData at this position
// - replace the contents of the view with that itemsData
viewHolder.image_view.setImageResource(itemList.get(position).getPhoto());
viewHolder.image_name.setText(itemList.get(position).getName());
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return this.itemList.size();
}
public CardViewDataAdapter(Context context, List<ItemObject> itemList) {
this.itemList = itemList;
}
}
MainActivity.java代码:
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);
// use a GridLayout manager
mLayoutManager = new GridLayoutManager(this, 2);
mRecyclerView.setLayoutManager(mLayoutManager);
// specify an adapter
List<ItemObject> rowListItem = getAllItemList();
mAdapter = new CardViewDataAdapter(MainActivity.this, rowListItem);
mRecyclerView.setAdapter(mAdapter);
}
@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();
if (id == R.id.action_settings) {
Toast.makeText(getApplicationContext(), "Settings Clicked",
Toast.LENGTH_SHORT).show();
return true;
/* } else if (id == R.id.action_search) {
Toast.makeText(getApplicationContext(), "Search Clicked",
Toast.LENGTH_SHORT).show();
return true;*/
}
return super.onOptionsItemSelected(item);
}
//Handles the references to items displayed on each cards
private List<ItemObject> getAllItemList() {
List<ItemObject> allItems = new ArrayList<ItemObject>();
allItems.add(new ItemObject("Fire", R.drawable.fire));
allItems.add(new ItemObject("Ambulance", R.drawable.ambulance));
allItems.add(new ItemObject("Police", R.drawable.police));
allItems.add(new ItemObject("AntiSquad", R.drawable.police));
return allItems;
}
}
itemsObject.java代码:
public class ItemObject {
private String name;
private int photo;
public ItemObject(String name, int photo) {
this.name = name;
this.photo = photo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPhoto() {
return photo;
}
public void setPhoto(int photo) {
this.photo = photo;
}
}
cardview_row.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!-- CardView with customized attributes -->
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="170dp"
android:layout_gravity="center_horizontal"
android:layout_margin="10dp"
cardview:cardCornerRadius="20dp"
cardview:cardElevation="90dp"
android:elevation="2dp"
android:background="@drawable/myrect"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp">
<ImageView
android:id="@+id/image_view"
android:layout_width="fill_parent"
android:layout_height="120dp"
/>
<TextView
android:id="@+id/image_name"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#4757b3"
android:textColor="@android:color/white"
android:fontFamily="sans-serif-condensed"
android:textSize="20sp"
android:textAlignment="center" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
最佳答案
看起来你有一个上下文
问题。
要获取上下文,您需要将代码更改为如下所示:
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:123456789"));
view.getContext().startActivity(callIntent);
关于Android 如何使用 CardView OnClick 方法调用电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37243184/
当用户按下 FAB 时,一个 cardview 被添加到 recyclerview。每个卡片 View 内都有一个复选框。我想这样做,以便当用户勾选特定卡片 View 中的复选框时,该特定卡片 Vie
我有一个 CardView inside 另一个 CardView,但是 child CardView周围没有阴影。知道为什么吗?
我发现 CardView 的影子有非常奇怪的行为。我刚开始开发一个应用程序,绝对没有任何业务逻辑或类似的东西。但无论如何…… 我只是在 fragment 布局中添加了几个 View ,然后发现每次屏幕
我有一个 CardView,里面有一些圆角半径,它是三个相对布局。当我设置顶部与 CardView 顶部对齐的布局背景时,圆角半径丢失。 如何保持圆角半径? 代码:
在我更新了我的 android studio 3.3 并将我的项目迁移到 androidx 工件后,我的卡片 View 高程停止工作并显示阴影。
我正在使用 CardView 在可滚动 GridLayout 中显示我的 ImageView 。但是,当我转到“设计”选项卡时,它看起来很好,但当我在设备上运行时,它看起来不同。 在布局上它显示为 当
这是代码 fragment : 这是最终结果: 我想要的是嵌套圆角半径的外观,它也会影响 ImageView。我已经为两个卡片 View 提供了 corner
一个简单的问题,我使用 CardView 向 Button 添加高度,但是在分配 cardElevation 时,CardView 在内部绘制了一个框本身,我尝试切换海拔和其他属性,我可以通过增加 c
我想在点击卡片 View 时添加链式 react ,奇怪的是它没有出现? 这里可能出了什么问题? //我有一个线性布局,在 cardview 中有三个 textview。 回收 View :
我是 android 的新手,使用 Kotlin 编写应用程序并使用 Anko 生成 View 。 我想使用 cardView 的 anko 版本,但 android studio 以红色突出显示错误
我的卡片 View 中有两个 ImageView ,现在如何设置 OnClickListeners 以便我可以知道在哪个卡片 View 中选择了哪个按钮。 im1 和 im2 是我的可点击 Image
我在编辑包含 Relativelayout 的 Cardview 中的 RelativeLayout 时搞砸了! ConstraintLayout 会将相对布局的 wrap_content 更改为 0
我想弄清楚如何在创建并传递 CardView 后修改某个 ImageView 或 TextView 小部件到主 RecyclerView。 所以我的代码如下: 首先是保存项目的类: public c
我在 CardView 中有一个 RecyclerView 但我有问题,我在 RecyclerView 底部有 2 个按钮,所以我把它们放在RelativeLayout 的末尾并给它们 parent_
如何在下面的屏幕截图中将 cardview 放在父级 cardview 之上? 当我将图像放入 CardView 时导致我的视图损坏 正确的观点必须是: 我的 xml 布局:
我正在尝试实现 this图书馆。此处编写的示例代码是用 Kotlin 编写的,但我的项目是用 Java 编写的,因此我尝试将代码从 Kotlin 转换为 Java。但是我收到以下错误: android
这是上下文中卡片 View 的布局: 我只希望图标 (imageview) 可以点击。这个 card
我有一个卡片 View ,里面有一个 ListView 。我也需要 ListView 中的点击项,我也希望能够使用 ontouchlistener 移动整个卡片 View 。我在卡片 View 上设置
我正在制作一个项目,但是当我编译我的项目并在 Android Studio 上启动应用程序时,我的应用程序崩溃了。 在我的 Logcat 错误中 Process: com.passionategeek
当我弄清楚 Material 样式时,我错过了必须为卡片 View 添加支持库依赖项的部分。相反,我使用了一个简单的标签。 它没有引发错误,甚至设计预览也显示了其中的卡片。但是当我在 Android
我是一名优秀的程序员,十分优秀!