码迷,mamicode.com
首页 > 移动开发 > 详细

简单拨号器(Android)

时间:2016-02-09 21:04:26      阅读:340      评论:0      收藏:0      [点我收藏+]

标签:

感受:

1.了解了intent中的action和Uri.

2.了解了向下一个活动传递数据。

3.了解了内容提供器。

4.了解自定义适配器。

4.其实T9拨号器和简单计算器原理一样。

技术分享

技术分享

技术分享

代码:

MainActivity:

技术分享
package com.example.contactstest;

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




import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.app.Activity;
import android.app.DownloadManager.Request;
import android.content.Intent;
import android.database.Cursor;

public class MainActivity extends Activity {

    ListView contactsView;

    List<Contact> contactsList = new ArrayList<Contact>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        contactsView = (ListView) findViewById(R.id.contacts_view);
        Contact_item adapter = new Contact_item(this, R.layout.item, contactsList);
        contactsView.setAdapter(adapter);
        readContacts();
        contactsView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Contact fruit = contactsList.get(position);
                String data = fruit.getNumber();
                Intent i = new Intent(MainActivity.this,Tel.class);
                i.putExtra("Tel_number", data);
                startActivity(i);
            }
        });
        
    }

    private void readContacts() {
        Cursor cursor = null;
        try {
            cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null, null, null, null);
            while (cursor.moveToNext()) {
             String displayName = cursor.getString(cursor
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                String number = cursor.getString(cursor
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                Log.d("MainActivity", "name is " + displayName);
                Contact a = new Contact(displayName,number);
                contactsList.add(a);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

}
View Code

Tel:

技术分享
package com.example.contactstest;



import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;



public class Tel extends Activity{
    
    String number;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.tel);

        Intent intent = getIntent();
        number = intent.getStringExtra("Tel_number");
        Button btnCall = (Button) findViewById(R.id.btn_call);
        EditText etNumber = (EditText)findViewById(R.id.number);
        etNumber.setText(number);
        etNumber.setSelection(etNumber.getText().length());//把光标定位到最后

        btnCall.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                //System.out.println("MainUI2 拨打电话.");
                //Log.d("Tel", number);
                call();
            }
        });

    }

    private void call(){

        //Log.d("MainUI", "拨打电话");

        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:"+number));
        startActivity(intent);

    }
}
View Code

Contact:

技术分享
package com.example.contactstest;

public class Contact {

    private String name;

    private String number;

    public Contact(String name, String number) {
        this.name = name;
        this.number = number;
    }

    public String getName() {
        return name;
    }

    public String getNumber() {
        return number;
    }
}
View Code

Contact_item:

技术分享
package com.example.contactstest;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;



public class Contact_item extends ArrayAdapter<Contact>{
    

        private int resourceId;

        public Contact_item(Context context, int textViewResourceId,
                List<Contact> objects) {
            super(context, textViewResourceId, objects);
            resourceId = textViewResourceId;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Contact fruit = getItem(position);
            View view;
            ViewHolder viewHolder;
            if (convertView == null) {
                view = LayoutInflater.from(getContext()).inflate(resourceId, null);
                viewHolder = new ViewHolder();
                viewHolder.fruitNumber = (TextView) view.findViewById(R.id.number);
                viewHolder.fruitName = (TextView) view.findViewById(R.id.name);
                view.setTag(viewHolder);
            } else {
                view = convertView;
                viewHolder = (ViewHolder) view.getTag();
            }
            viewHolder.fruitNumber.setText(fruit.getNumber());
            viewHolder.fruitName.setText(fruit.getName());
            return view;
        }
        
        class ViewHolder {
            
            TextView fruitNumber;
            
            TextView fruitName;
            
        }

    }
View Code

 

简单拨号器(Android)

标签:

原文地址:http://www.cnblogs.com/ZP-Better/p/5185675.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!