鍍金池/ 教程/ Android/ 百戰(zhàn)經(jīng)典第二十戰(zhàn)-ListView中點(diǎn)擊button跳轉(zhuǎn)到撥號(hào)界面實(shí)例
百戰(zhàn)經(jīng)典第二十戰(zhàn)-ListView中點(diǎn)擊button跳轉(zhuǎn)到撥號(hào)界面實(shí)例
百戰(zhàn)經(jīng)典第十一戰(zhàn)-GridView動(dòng)態(tài)添加Item
百戰(zhàn)經(jīng)典第二戰(zhàn)-好玩的Spinner控件
百戰(zhàn)經(jīng)典第五戰(zhàn)-各種對(duì)話框Dialog精彩薈萃
百戰(zhàn)經(jīng)典第八戰(zhàn)-BitmapFactory.Options對(duì)資源圖片進(jìn)行縮放
百戰(zhàn)經(jīng)典第四戰(zhàn)-玩轉(zhuǎn)ListView
百戰(zhàn)經(jīng)典第十五-竊聽(tīng)風(fēng)云之短信監(jiān)聽(tīng)
前言
百戰(zhàn)經(jīng)典第十四戰(zhàn)-網(wǎng)絡(luò)交互,基于Baas用戶表查詢功能實(shí)現(xiàn)
百戰(zhàn)經(jīng)典第九戰(zhàn)-ViewFlipper實(shí)現(xiàn)幻燈效果
百戰(zhàn)經(jīng)典第三戰(zhàn)-實(shí)現(xiàn)畫(huà)圖板
百戰(zhàn)經(jīng)典第十七戰(zhàn)-基于加速度傳感器的搖一搖功能實(shí)例
百戰(zhàn)經(jīng)典第十戰(zhàn)-LayoutAnimation布局動(dòng)畫(huà)效果
百戰(zhàn)經(jīng)典第七戰(zhàn)-顯示倒計(jì)時(shí)的Button按鈕
百戰(zhàn)經(jīng)典第六戰(zhàn)-Activity啟動(dòng)模式小樣
百戰(zhàn)經(jīng)典第十二戰(zhàn)-GridView動(dòng)態(tài)刪除Item
百戰(zhàn)經(jīng)典第十六戰(zhàn)-圖片或頭像設(shè)置功能
百戰(zhàn)經(jīng)典第十九戰(zhàn)-短信監(jiān)聽(tīng)實(shí)現(xiàn)驗(yàn)證碼自動(dòng)填入
百戰(zhàn)經(jīng)典第一戰(zhàn)—聽(tīng)話的TextView
百戰(zhàn)經(jīng)典第十八戰(zhàn)-自定義控件實(shí)現(xiàn)一鍵清空輸入框
百戰(zhàn)經(jīng)典第十三戰(zhàn)-網(wǎng)絡(luò)交互,基于Baas實(shí)現(xiàn)用戶注冊(cè)功能

百戰(zhàn)經(jīng)典第二十戰(zhàn)-ListView中點(diǎn)擊button跳轉(zhuǎn)到撥號(hào)界面實(shí)例

最近討論了一個(gè)項(xiàng)目需求,在ListView的Item中放置了一個(gè)類(lèi)似電話的圖標(biāo),點(diǎn)擊圖標(biāo)可以將號(hào)碼調(diào)到撥號(hào)界面。實(shí)現(xiàn)起來(lái)很是容易,原理也易懂,較為實(shí)用,項(xiàng)目中有需要的可以直接引入。 我模擬了一個(gè)簡(jiǎn)單的demo.代碼如下:

1.ListAdapter.java:

package com.example.listviewphone;
//省略import
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()));  //跳轉(zhuǎn)到用戶界面較為溫和,推薦使用!                   context.startActivity(intent);  
            }
        });
        return convertView;
    }
    class ViewHolder {
        private TextView mPhoneTextView;
        private TextView mTitleLisTextView;
        ViewHolder() {
        }
    }
}

ListAdapter 繼承自BaseAdapter,構(gòu)造方法傳入上下文對(duì)象context和數(shù)據(jù)List-tests。覆寫(xiě)了getCount方法,返回?cái)?shù)據(jù)個(gè)數(shù),覆寫(xiě)了getItem返回指定下表對(duì)象,覆寫(xiě)了getItemId返回指定欄目下標(biāo)。覆寫(xiě)了getView方法,返回view,在getView方法中實(shí)現(xiàn)了子欄目的單擊事件監(jiān)聽(tīng),結(jié)合系統(tǒng)的Intent.ACTION_DIAL,進(jìn)行電話功能的調(diào)取。

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;
}
}

為了便于操作,封裝了Test類(lèi)。

3.MainActivity.java:

package com.example.listviewphone;
//省略了import
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);
        }
    }
}

下面是簡(jiǎn)單的兩個(gè)布局文件:

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>

運(yùn)行實(shí)例如下:

這里寫(xiě)圖片描述

點(diǎn)擊一個(gè):

這里寫(xiě)圖片描述

成功跳轉(zhuǎn)到撥號(hào)界面,并將對(duì)應(yīng)的電話號(hào)碼傳了過(guò)來(lái)。