android---简单的拨号器
8.19更新,这里的adapter中的getview优化并不是很好,最好的方法是用静态内部类,后面文章有
利用前面的listview和intent做出来的简单的拨号器,主要是可以直接拨打常用的急救电话
mainactivity:
package com.niuli.demo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private Button specialcall;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
specialcall = (Button) findViewById(R.id.button1);
specialcall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this,Listviewdemo.class);
startActivity(intent);
}
});
}
}
listviewadapter:
package com.niuli.demo;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class Listviewadapter extends BaseAdapter {
private Context context = null;
public Listviewadapter(Context context) {
this.context = context;
}
public Context getContext() {
return context;
}
Listviewdata Data[] = new Listviewdata[]{
new Listviewdata("警匪", "110"),
new Listviewdata("急救", "120"),
new Listviewdata("火警", "119"),
new Listviewdata("交通事故", "122"),
new Listviewdata("天气预报", "12121"),
new Listviewdata("森林火警", "95119"),
new Listviewdata("水上求救", "12395")
};
@Override
public int getCount() {
return Data.length;
}
@Override
public Object getItem(int arg0) {
return Data[arg0];
}
@Override
public long getItemId(int arg0) {
return arg0;
}
/**
* 使用自定义的xml来显示,需要LinearLayout的解析器来解析读取
* 重点内容,这里的优化并不是很好,最好的方法是用静态内部类,后面文章有
*/
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
LinearLayout ll = null;
if (arg1!=null) {
ll = (LinearLayout) arg1;
}else {
//这里需要得到context的实例来完成解析
ll = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.listviewdemo, null);
}
Listviewdata data = Data[arg0];
ImageView icon = (ImageView) ll.findViewById(R.id.imageView1);
TextView textView1 = (TextView) ll.findViewById(R.id.textView1);
TextView textView2 = (TextView) ll.findViewById(R.id.textView2);
icon.setImageResource(data.getIconm());
textView1.setText(data.getDec());
textView2.setText(data.getNumber());
return ll;
}
}
listviewdata:
package com.niuli.demo;
public class Listviewdata {
public Listviewdata(String dec,String number) {
this.number = number;
this.dec = dec;
}
private String number;
private String dec;
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getDec() {
return dec;
}
public void setDec(String dec) {
this.dec = dec;
}
public int getIconm() {
return iconm;
}
public void setIconm(int iconm) {
this.iconm = iconm;
}
private int iconm = R.drawable.img1;
}
listviewdemo:
package com.niuli.demo;
import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
public class Listviewdemo extends ListActivity{
private Intent intent;
String uri = "tel:";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new Listviewadapter(this));
intent = new Intent(Intent.ACTION_VIEW);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
switch (position) {
case 0:
intent.setData(Uri.parse(uri+"110"));
startActivity(intent);
break;
case 1:
intent.setData(Uri.parse(uri+"120"));
startActivity(intent);
break;
case 2:
intent.setData(Uri.parse(uri+"119"));
startActivity(intent);
break;
case 3:
intent.setData(Uri.parse(uri+"122"));
startActivity(intent);
break;
case 4:
intent.setData(Uri.parse(uri+"12121"));
startActivity(intent);
break;
case 5:
intent.setData(Uri.parse(uri+"95119"));
startActivity(intent);
break;
case 6:
intent.setData(Uri.parse(uri+"12395"));
startActivity(intent);
break;
default:
break;
}
super.onListItemClick(l, v, position, id);
}
}
listviewdemo.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="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
版权声明:本文为博主原创文章,未经博主允许不得转载。