Android实战简易教程

编程入门 行业动态 更新时间:2024-10-21 17:30:29

Android实战<a href=https://www.elefans.com/category/jswz/34/1769136.html style=简易教程"/>

Android实战简易教程

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

package com.example.listviewphone;import java.util.List;import android.content.Context;
import android.content.Intent;
import android.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);}@Overridepublic int getCount() {return tests.size();}@Overridepublic Object getItem(int position) {return tests.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic 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() {@Overridepublic 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;@Overrideprotected 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=""xmlns: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=""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 跳到拨打界面,推荐使用

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

更多推荐

Android实战简易教程

本文发布于:2024-03-23 23:48:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1744278.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:简易   实战   教程   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!