0
回答
Andorid开发拨号程序的完善
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

我自己写了一个拨号程序 能拨号也可以通过菜单显示联系人 但是不能通过单击联系人拨打号码 而且不能设置联系人的背景图片 以及拨打号码时的背景图片的修改 求高手指点 我是新手 这个Eclipse是最新版的可能与大家的有点不一样 但大致相同 欢迎各位大神一起来交流 最好能够实现单击联系人拨打电话。。。其余的不强求

这是我的程序结构

下面是我的MainActivity.java代码

package cn.com.a121;

import java.util.ArrayList;
import java.util.HashMap;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        super.onCreateOptionsMenu(menu);
        final EditText a2;
		final Button a3;
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        a2 = (EditText) this.findViewById(R.id.a2);
		a3 = (Button) this.findViewById(R.id.a3);
		a3.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				String sss = a2.getText().toString();
				if (!("").equals(sss)) {
					Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+sss));
					startActivity(intent);
				}
			}
		});
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
        	showListView();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public void sqqq() {
		
		TextView tv = new TextView(this);
		tv.setText("此功能未完善");
		setContentView(tv);
	}
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }
    private void showListView(){
    	  ListView listView = new ListView(this);

    	  ArrayList<HashMap<String, String>> list = getPeopleInPhone2();
    	  SimpleAdapter adapter = new SimpleAdapter(
    	         this, 
    	         list, 
    	         android.R.layout.simple_list_item_2, 
    	         new String[] {"peopleName", "phoneNum"}, 
    	         new int[]{android.R.id.text1, android.R.id.text2}
    	        );
    	  listView.setAdapter(adapter);
    	  setContentView(listView);
    	 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    		  public void onItemClick(AdapterView<?> parent, View view,
    				  int position, long id) {
    			  Toast.makeText(MainActivity.this, "你点击的是第" + (id+1) + "项",  
                          Toast.LENGTH_SHORT).show();
    			  sqqq();
    				  // TODO Auto-generated method stub
    				  }
    	  });
    	 }

    	 private ArrayList<HashMap<String, String>> getPeopleInPhone2(){
    	  ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();

    	        Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);  // 获取手机联系人
    	  while (cursor.moveToNext()) {
    	   HashMap<String, String> map = new HashMap<String, String>();

    	   int indexPeopleName = cursor.getColumnIndex(Phone.DISPLAY_NAME);  // people name
    	   int indexPhoneNum = cursor.getColumnIndex(Phone.NUMBER);    // phone number
    	   String strPeopleName = cursor.getString(indexPeopleName);
    	   String strPhoneNum = cursor.getString(indexPhoneNum);
    	   map.put("peopleName", strPeopleName);
    	   map.put("phoneNum", strPhoneNum);
    	   list.add(map);
    	  }
    	        if(!cursor.isClosed()){
    	         cursor.close();
    	         cursor = null;
    	        }

    	        return list;
    	 }

}



下面是我的fragment_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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/aaa"
    tools:context="cn.com.helloandroid.MainActivity$PlaceholderFragment" >

   <TextView
       android:id="@+id/a1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
   <EditText
       android:id="@+id/a2"
       android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:layout_below="@+id/a1"
        android:inputType="phone"
        android:singleLine="true"
        android:background="#00000000"
       />
   <Button
        android:id="@+id/a3"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/a2"
        android:text="@string/abc"
        android:layout_alignParentRight="true"
        
         />

</RelativeLayout>



下面是strings.xml代码
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">李行拨号系统</string>
    <string name="hello_world">请输入电话号码</string>
    <string name="action_settings">联系人</string>
    <string name="abc">拨号</string>
    

</resources>

求高手指点 在下不甚感激



举报
saa
发帖于4年前 0回/143阅
顶部