Android实战简易教程-第三十七枪(ListView中点击button跳转到拨号界面实例)

最近讨论了一个项目需求,在ListView的Item中放置了一个类似电话的图标,点击图标可以将号码调到拨号界面。实现起来很是容易,原理也易懂,较为实用,项目中有需要的可以直接引入。
我模拟了一个简单的demo.代码如下:
1.ListAdapter.java:

package com.example.listviewphone;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ListAdapter extends BaseAdapter {
    private List<Test> tests;
    private Context context;
    LayoutInflater layoutInflater;
    public ListAdapter(Context context,List<Test> tests){
        this.tests=tests;
        this.context=context;
        layoutInflater=LayoutInflater.from(context);

    }

    @Override
    public int getCount() {
        return tests.size();
    }

    @Override
    public Object getItem(int position) {
        return tests.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder=null;
        if(convertView==null){
            viewHolder=new ViewHolder();
            convertView=layoutInflater.inflate(R.layout.item_list, null);
            viewHolder.mTitleLisTextView=(TextView)convertView.findViewById(R.id.tv_title_list);
            viewHolder.mPhoneTextView=(TextView)convertView.findViewById(R.id.tv_phone_list);
            convertView.setTag(viewHolder);

        }else {
            viewHolder=(ViewHolder) convertView.getTag();
        }

        viewHolder.mTitleLisTextView.setText(tests.get(position).getTitle_lost());
        viewHolder.mPhoneTextView.setText(tests.get(position).getPhone_lost());
        viewHolder.mPhoneTextView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                 // Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+tests.get(position).getPhone_lost()));  //直接拨打电话,较为暴利,慎用!
                Intent intent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:"+tests.get(position).getPhone_lost()));  //跳转到用户界面较为温和,推荐使用!
                    context.startActivity(intent);  
            }
        });



        return convertView;
    }

    class ViewHolder {
        private TextView mPhoneTextView;
        private TextView mTitleLisTextView;

        ViewHolder() {
        }
    }





}

2.javabean—Test.java:

package com.example.listviewphone;

public class Test 
{
  private String content_test;
  private String phone_test;
  private String title_test;
  private String username;
public String getContent_test() {
    return content_test;
}
public void setContent_test(String content_test) {
    this.content_test = content_test;
}
public String getPhone_test() {
    return phone_test;
}
public void setPhone_test(String phone_test) {
    this.phone_test = phone_test;
}
public String getTitle_test() {
    return title_test;
}
public void setTitle_test(String title_test) {
    this.title_test = title_test;
}
public String getUsername() {
    return username;
}
public void setUsername(String username) {
    this.username = username;
}


}

3.MainActivity.java:

package com.example.listviewphone;

import java.util.ArrayList;
import java.util.List;

import javax.security.auth.PrivateCredentialPermission;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.widget.ListView;

public class MainActivity extends Activity {

    private ListView mListView;
    private ListAdapter adapter;
    private List<Test> tests;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        mListView=(ListView)findViewById(R.id.listview);
        initDatas();
        adapter=new ListAdapter(this, tests);
        mListView.setAdapter(adapter);

    }
    private void initDatas() {
        tests=new ArrayList<Test>();

        for (int i = 0; i < 30; i++) {
            Test test =new Test();
            test.setTitle_test("电话");
            test.setPhone_test("123456789"+i);
            tests.add(test);
        }





    }


}

下面是简单的两个布局文件:
1.activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

2.item_list.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:background="#ffffff"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/tv_title_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="电话"
        android:singleLine="true"
        >
    </TextView>



    <TextView
        android:id="@+id/tv_phone_list"

        android:textSize="12sp"
       android:layout_height="wrap_content"
       android:layout_width="wrap_content"
        android:background="@color/green"
        android:text="138024249542"
        android:padding="4dp"
        android:layout_marginLeft="140dp"
        android:drawableLeft="@drawable/icon_photo" >
    </TextView>

</LinearLayout>

运行实例如下:
这里写图片描述
这里写图片描述

这里写图片描述
总结:有两种跳转,1-Intent.ACTION_CALL 直接拨打电话,较为暴利慎用;
2-Intent.ACTION_DIAL 跳到拨打界面,推荐使用

有需要的引入自己的项目吧,喜欢的朋友关注个吧! 多谢支持!

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值