安卓应用中,能够实现拨打电话功能,下面就进行演示
首先我们建立一个安卓项目,然后进行书写,其中在布局文件中,我们只需要简单的设计就可以了,只需一个按钮和文本编辑框
下面是源码:
<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:orientation="vertical" android:alpha="0.7" android:background="@drawable/ljo" > <AutoCompleteTextView android:id="@+id/editText_number" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="41dp" android:ems="10" android:completionThreshold="1" android:inputType="phone" > </AutoCompleteTextView> <Button android:id="@+id/button_dail" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/editText_number" android:layout_marginTop="97dp" android:text="@string/dail" /> </RelativeLayout>在代码中,我们需要将
android:text="@string/dail"中的dail定义到string.xml中,布局文件的界面如下图所示
在写完布局文件之后,我们进行对MainActivity类的实现,首先我们需要绑定布局文件,这个就在oncreate()函数中实现,然后在获取在文本框中输入的数字电话号码,接着就点击拔打电话按钮实现功能,当然,在点击时就要设置监听事件的,代码如下所示
public class MainActivity_call extends Activity implements OnClickListener { private Button btn_dail;EditText editText_number_m; private AutoCompleteTextView autoCompleteTextView_number; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_call); btn_dail=(Button)this.findViewById(R.id.button_dail); btn_dail.setOnClickListener(MainActivity_call.this); autoCompleteTextView_number=(AutoCompleteTextView) findViewById(R.id.editText_number); ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,numbers); autoCompleteTextView_number.setAdapter(adapter); editText_number_m=(EditText)MainActivity_call.this.findViewById(R.id.editText_number); } private String[] numbers={"1008611","10086","1234567890","9876543210","725610987678","226610"}; @Override public void onClick(View v) { String number=editText_number_m.getText().toString(); //System.out.println("first"); if(number.length()!=0) { Intent intent=new Intent("android.intent.action.CALL",Uri.parse("tel:"+number)); startActivity(intent); } else { Toast.makeText(this,"请输入号码...",Toast.LENGTH_SHORT).show(); } } }