标签:android style class blog code java
首先,先创建一个安卓项目(我的版本号是4.4.2的),名字为"电话拨号器",创建的时候点击"clipart",如图:
然后在res目录下找到layout目录,找到activity_main.xml或fragment_main.xml,在里面输入或拖拽button
<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.csdn.www.MainActivity$PlaceholderFragment" > <EditText android:id="@+id/edit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="16dp" android:ems="10" android:inputType="phone" /> <Button android:id="@+id/btn_call" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/editText1" android:layout_centerHorizontal="true" android:layout_marginTop="125dp" android:text="@string/call" /> </RelativeLayout>
package com.csdn.www; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; import android.text.TextUtils; import android.content.Intent; 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.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import android.os.Build; public class MainActivity extends ActionBarActivity { private Button btn; private EditText editText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_main); btn=(Button) this.findViewById(R.id.btn_call); editText=(EditText) this.findViewById(R.id.edit); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //.trim()防止里面有空格 String number=editText.getText().toString().trim(); //假设文本为空,则显示错误 if(TextUtils.isEmpty(number)){ Toast.makeText(MainActivity.this,"号码不能为空",Toast.LENGTH_SHORT).show(); return; } //意图 想干什么事 Intent intent=new Intent(); //拨打电话的action intent.setAction(intent.ACTION_CALL); //uri intent.setData(Uri.parse("tel:::"+number)); startActivity(intent); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }
找到文件夹下AndroidManifest.xml文件,打开,如图:
然后就能够打电话了!!!
初识安卓小程序(Android电话拨号器),布布扣,bubuko.com
标签:android style class blog code java
原文地址:http://www.cnblogs.com/hrhguanli/p/3772846.html