Before learning the helloworld project structure 3.Android-ADT's introduction , the chapter will be to write a simple phone dialer program.
Implementation steps are as follows:
- 1. Create a project
- 2. Write layout / activity_main.xml layout file (painting ui), to achieve display
- 3. Write MainActivity.java code to achieve specific logical functions
- 4. Start APP see results
1. Create a project
After setting the project name, has been the point next, until the finish.
The created layout / activity_main.xml layout is shown as follows:
Xml corresponding layout is shown below:
<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_horizontal_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </RelativeLayout>
As long as we can see layout_width and layout_height value of the property values are written directly, the other is used for all the index mark (@ XXX / YYYY) , these addresses are present R.java index pointer, the index and the corresponding content will be present values / XXXs.xml in (such @dimen located dimens.xml, while the string is located in the strings.xml)
To @ string / hello_world example:
In the R.java
The values / strings.xml in
So hello world is displayed on the TextView control!
Why do we need an index, rather than directly written as: android: "! Hello world" text =
A: To an international language, because the value of the index point can be replaced, we use the default values are indexed content under the / xxx.xml, if we set to English, then it can be changed to values-en / xxx. English indexed content in xml (learning behind international chapters).
And writing, it can not achieve language internationalization.
Analysis of the property value RelativeLayout
- RelativeLayout :表示相对布局的意思,这里表示将当前界面设置为相对布局.
- android:layout_width和android:layout_height: 设置布局的宽高,对于界面布局而言,这里只能设置为"match_parent"(填满父控件)。
- android:paddingXXX: 内边框,表示该界面布局内部的控件距离本身边缘有多远
- tools:context=".MainActivity" : 表示这个xml布局文件关联的是src/MainActivity.java类(用来实现具体逻辑功能)
分析TextView之属性值
TextView用于显示常规文字(不能被修改)的控件
注意: 要想显示控件,必须指定layout_width和layout_height(宽高)
对于控件的宽高设置有三种值可填,如下图所示:
"fill_parent" //强制让它布满填满父控件的空白 "wrap_content" //设置控件高度刚好能够显示当前内容的大小 "match_parent" //和fill_parent是一个意思,他们的定义本质是一样的,均为-1,只是换了别名,为了命名更准确些, 官方更推荐使用match_parent
2. 写layout/activity_main.xml布局文件(画ui)
最终界面如下图所示:
3.写MainActivity.java代码
在MainActivity.java里主要创建了一个MainActivity类,继承于Activity,如下图所示:
接下来我们便来介绍Activity.
3.1 Activity介绍
一个活动是一个单一的,集中的事情,用户可以做。几乎所有的活动都与用户交互,因此Activity类负责为您创建一个窗口,您可以在其中放置带有setContentView(View)的UI。虽然活动通常作为全屏窗口呈现给用户,但它们也可以以其他方式使用:作为浮动窗口(通过windowisfloat设置的主题)或嵌入到另一个活动中(使用ActivityGroup)。有两个方法,几乎所有子类的活动将实现:
- onCreate(Bundle) :用于初始化activity的地方,当activity创建的时候会被调用,这里通常使用定义UI的布局资源调用setContentView(int),并使用findViewById(int)检索UI中需要以编程方式交互的小部件。
- onPause():是处理用户离开活动的地方。最重要的是,此时用户所做的任何更改都应该提交(通常是提交给持有数据的ContentProvider)。
3.2 写MainActivity.java
package com.example.phonecall; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.text.TextUtils; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; //添加继承OnClickListener点击事件类,然后覆写onClick() public class MainActivity extends Activity implements OnClickListener { private EditText et_number; //文本框对象 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //加载要显示的界面UI //1.找到EditText,然后设置按键按下监听 et_number = (EditText) findViewById(R.id.editText1); Button bt_call = (Button) findViewById(R.id.button1); bt_call.setOnClickListener(MainActivity.this); //MainActivity.this表示MainActivity类 } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); //点击菜单按钮时,则显示菜单界面 return true; } @Override public void onClick(View v) { System.out.println("on click"); String number = et_number.getText().toString().trim(); //2.获取内容 if(TextUtils.isEmpty(number)){ Toast.makeText(MainActivity.this, "内容不能为空", Toast.LENGTH_SHORT).show(); //显示一个提示的浮动显示块 }else{ Intent intent = new Intent(); intent.setAction(intent.ACTION_CALL); //设置intent要执行的动作, ACTION_CALL表示打电话 intent.setData(Uri.parse("tel:"+number)); //uri和url类似,但是uri可以自定义多个协议,”tel:“+number表示给number打电话 startActivity(intent); //通过intent(意图)来实现应用间的交互与通讯,从而实现人的意图 } } }
注意 导入包点击监听事件的包时,选择第二个"android.view.view" :
否则的话,onClick(View v)会变为onClick(DialogInterface dialog,int which)。
3.3 然后添加打电话申请权限(用于是否支持从应用中直接打电话)
在AndroidManifest清单->Permissions里选择Users Permission.
然后添加android.permission.CALL_PHONE
4.然后启动APP