简体   繁体   English

如何在 ListView 中实现呼叫按钮?

[英]How to to implement a call button in the ListView?

I am working on an android project.我正在开发一个 android 项目。 It is blood donor searching app.它是献血者搜索应用程序。 When the user searches for blood donor, the current location of the user is identified by the GPS.当用户搜索献血者时,用户的当前位置由GPS识别。 The coordinates are then compared with the details in the database.然后将坐标与数据库中的详细信息进行比较。 Then matching donor contact details are retrieved.然后检索匹配的捐助者联系方式。

The details are printed in the format of a ListView.详细信息以 ListView 的格式打印。 I want to provide a calling option with these details.我想提供包含这些详细信息的呼叫选项。 for eg.例如。 when the details are displayed, the user can long press on the phone number to call the donor.当显示详细信息时,用户可以长按电话号码呼叫捐赠者。 can this be implemented?这可以实施吗?

How can I implement this?我该如何实施? Is it possible是否可以

Implement setOnLongClickListener on your Phone number textView.在您的电话号码 textView 上实现 setOnLongClickListener。 For example,例如,

txtPhoneNumber.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
         Uri number = Uri.parse("tel:"+ txtPhoneNumber.getText().toString);
         Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
         startActivity(callIntent);

         return true;
        }
    });

And the permission <uses-permission android:name="android.permission.CALL_PHONE" />和权限<uses-permission android:name="android.permission.CALL_PHONE" />

BTW are you using a custom adapter for your listview?顺便说一句,您是否在为您的列表视图使用自定义适配器?

listView.setOnItemLongClickListener(new OnItemLongClickListener() {  

      public boolean onItemLongClick(AdapterView<?> arg0, View v,  
              int position, long arg3) {  
          Toast.makeText(ListViewTestActivity.this, "content"+position, Toast.LENGTH_LONG).show();  
          return false;  
      }  
  });  

For TextView long click:对于 TextView 长按:

textView.setOnLongClickListener(new OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {
        callNumber(textView.getText().toString().trim());
        return true;
    }

}); });

Here is the method with which you can make a call:这是您可以拨打电话的方法:

private void callNumber(String telephoneNumber) {
    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + telephoneNumber.replace("-", "")));
    startActivity(intent);
}

Dont forget to add permission in AndroidManifest.xml file:不要忘记在 AndroidManifest.xml 文件中添加权限:

<uses-permission android:name="android.permission.CALL_PHONE" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM