最近讨论了一个项目需求,在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 跳到拨打界面,推荐使用
有需要的引入自己的项目吧,喜欢的朋友关注个吧! 多谢支持!
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-06 22:29:48