The interface
Function is to achieve, there are two prompts for text box on the screen, enter the recording will be saved, when again will be prompted to enter
After adding input confirmation, in the following listview will be displayed,
Add a display a
When the phone number of clicks, the corresponding number will be dialed telephone
A total of three codes need to operate
Here is the code
itemm.xml Code
<?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" > <TextView android:id="@+id/name" android:text="电话号码" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:textColor="#000" android:textSize="20dp" /> <TextView android:text="空" android:id="@+id/number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000" android:textSize="20dp" android:layout_weight="1"/> </LinearLayout>
activity_main.xml Code
<?xml version="1.0" encoding="utf-8"?> <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" tools:context="com.example.xialm.autocomplete_2.MainActivity"> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> <LinearLayout android:id="@+id/l1" android:layout_below="@+id/tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="姓名" android:textSize="20dp" android:textColor="#000" /> <AutoCompleteTextView android:id="@+id/au_name" android:layout_below="@+id/tv" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:id="@+id/l2" android:layout_below="@+id/l1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="号码" android:textSize="20dp" android:textColor="#000" /> <AutoCompleteTextView android:id="@+id/au_number" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> <Button android:id="@+id/bu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/l2" android:text="点击添加" /> <TextView android:paddingTop="8dp" android:layout_below="@+id/bu" android:id="@+id/tv_jiang" android:text="自动提示条目库" android:textSize="30dp" android:textColor="#000" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/tv_jiang" android:fastScrollEnabled="true" > </ListView> </RelativeLayout>
logic code mainactivity
package com.example.xialm.autocomplete_2; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.content.SyncStatusObserver; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; import android.widget.Toast; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MainActivity extends AppCompatActivity { private AlertDialog.Builder myb; //放电话号码 private List<String> list1=new ArrayList<String>(); //放名字 private List<String> list2=new ArrayList<String>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //对Builder拿了一个实例 myb = new AlertDialog.Builder(MainActivity.this); //找到关心的控件 final AutoCompleteTextView auto_number = (AutoCompleteTextView)findViewById(R.id.au_number); final= Auto_name the AutoCompleteTextView (the AutoCompleteTextView) the findViewById (R.id.au_name); the Button Bu = (the Button) the findViewById (R.id.bu); Final the ListView LV = (the ListView) the findViewById (R.id.lv); // use android is inside a layout file bring the ArrayAdapter <String> Adapter = new new the ArrayAdapter <String> ( the this , android.R.layout.simple_dropdown_item_1line, List1); the ArrayAdapter <String> = adapter2 new new the ArrayAdapter <String> ( the this , android. R.layout.simple_dropdown_item_1line, list2); // to fill full-text frame to match the contents, the contents of which we put the adapter inside auto_number.setAdapter (adapter); auto_name.setAdapter (adapter2); lv.setAdapter ( new new myad ()); lv.setOnItemClickListener ( new new AdapterView.OnItemClickListener () { @Override public void <?> in the onItemClick (The AdapterView parent, View View, Final int position, Long ID) { Final String usenum = List2. GET (position); // add content inquiry prompted by Builder objects myb.setMessage ( "sure you want to call it" ); // set the button to display the success of the content and click event myb.setPositiveButton ( "yes", new new void DialogInterface.OnClickListener () { @Override public startActivity (intent);onClick (DialogInterface Dialog, int Which) { the Intent the Intent = new new the Intent (); // confirm the kind of intent is to call intent.setAction (Intent.ACTION_CALL); . Toast.makeText (MainActivity the this , "is calling" + usenum, Toast.LENGTH_SHORT) the .Show (); // determine the number of call intent.setData (Uri.parse ( "Tel:" + usenum)); // number may be replaced by a fixed telephone // open intended set the button to display the contents of failure and click event } }); // myb.setNegativeButton ( "not", new new DialogInterface.OnClickListener () { @Override public void onClick (DialogInterface Dialog, int Which) { Toast.makeText (MainActivity. the this , "Cancel success" , Toast.LENGTH_SHORT) .Show (); } }); // create dialog AlertDialog AA = myb.create (); // display a dialog AA.show (); } }); bu.setOnClickListener ( new new View.OnClickListener () { @Override public void the onClick (View V) { String S = auto_number.getText () toString ();. String S2 = auto_name.getText () toString ();. // After num operation then once adaptation can update ui list1.add (S); list2.add (s2); System.out.println ( "name:" + list2 + "Tel:" + List1); // auto.setAdapter (adapter); // using a layout file inside android bring their own ArrayAdapter <String> Adapter = new newThe ArrayAdapter <String> (the MainActivity. The this {, Android.R.layout.simple_dropdown_item_1line, List1); the ArrayAdapter <String> = adapter2 new new the ArrayAdapter <String> (the MainActivity. The this , android.R.layout.simple_dropdown_item_1line, List2); // to a completed text block matching content, content we adapter is placed inside auto_number.setAdapter (adapter); auto_name.setAdapter (adapter2); lv.setAdapter ( new new myad ()); } }); } // own definition of a class that inherits BaseAdapter public class myad the extends BaseAdapter Holder myholder; @Override public int getCount() { //创建多少个项目 return list1.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View view, ViewGroup parent) { if(view == null ) { // Create a new view objects // by a pump to a layout resource is converted to a view object view = View.inflate (MainActivity. the this , R.layout.itemm, null ); // layout file not only converted into view, and the last parameter is used to make a group, // can add just generated view came myholder = new new Holder (); myholder.tv_1 = (TextView) view.findViewById (R.id.name ); myholder.tv_2 = (TextView) view.findViewById (R.id.number); // pump of the second method //= LayoutInflater.from View (getApplicationContext ()) the inflate (R.layout.itemm, null);. // pump third method // LayoutInflater inflater so that = (LayoutInflater) the getSystemService (LAYOUT_INFLATER_SERVICE); // View = Inflater.inflate (R.layout.itemm, null); // set up a label, the label may carry any information view.setTag (myholder); } the else { // get the information again, the optimization of resources shared myholder = (Holder) view.getTag (); } myholder.tv_1.setText (list2.get (position)); / / myholder.tv_2.setText (NUM [position]); myholder.tv_2.setText (list1.get (position)); System.out.println ("Adapter to a name" + list2.get (position) + " \ n adapter obtained number" + list1.get (position)); return View; } } class Holder { the TextView TV_1; the TextView tv_2; } }