在D:\Android\Androidwork\phone\res\layout\activity_main.xml下进行布局:
效果图:
activity_main.xml文件:这个布局可以预览。
<!--线性布局-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/mobile"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/mobile"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
android:id="@+id/button"
/>
</LinearLayout>
D:\Android\Androidwork\phone\res\values\strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">拨号器</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="mobile">请输入手机号</string>
<string name="button">请拨打手机号</string>
</resources>
D:\Android\Androidwork\phone\gen\com\atguigu\phone\R.java:
package com.atguigu.phone;
public final class R {
public static final class id {
public static final int action_settings=0x7f080002;
public static final int button=0x7f080001;
public static final int mobile=0x7f080000;
}
}
D:\Android\Androidwork\phone\src\com\atguigu\phone\MainActivity.java:
package com.atguigu.phone;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button) this.findViewById(R.id.button);
//为按钮添加点击事件
button.setOnClickListener(new ButtonClickListener());
}
private final class ButtonClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
EditText mobileText=(EditText) findViewById(R.id.mobile);
String number=mobileText.getText().toString();
Intent intent=new Intent();
intent.setAction("android.intent.action.CALL");
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse("tel:"+number));
startActivity(intent);//方法内部会自动为Intent添加类别:android.intent.category.DEFAULT
}
}
}
Android内部软件的源代码下载方法:
是否安装这款软件的提示:
D:\Android\Androidwork\phone\AndroidManifest.xml:
<uses-permission android:name="android.permission.CALL_PHONE"/>
</manifest>