Android Development Demo: “DialANumber”

While I think the Android development examples are not really good and straight forward perhaps some of you come along with them. However, I would like to provide you a very very simple example here on how to write your own first Android program.

This is technically very easy. Once you got Eclipse and Android installed and running on your computer (I use a Mac OS Leopard powered MacBook but you can use a Microsoft Windows XP/Vista/etc. PC as well) you just start Eclipse. Now you just go to “New » Project... » Android » Android Project...” and enter the required data. Once you are done the result might look like this:

Android Development Demo: “DialANumber”

Now you can start writing and customize the default “Hello World” a.k.a. “Hello Android” app which just shows a text field. If you are really really lazy you could also copy/paste the following code:


package com.DialANumber;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class DialANumber extends Activity {
EditText mEditText_number = null;
LinearLayout mLinearLayout_no_button = null;
Button mButton_dial = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mLinearLayout_no_button = new LinearLayout(this);

mEditText_number = new EditText(this);
mEditText_number.setText("5551222");
mLinearLayout_no_button.addView(mEditText_number);

mButton_dial = new Button(this);
mButton_dial.setText("Dial!");
mLinearLayout_no_button.addView(mButton_dial);
mButton_dial.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
performDial();
}
});

setContentView(mLinearLayout_no_button);
}

public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_CALL) {
performDial();
return true;
}
return false;
}

public void performDial(){
if(mEditText_number!=null){
try {
startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + mEditText_number.getText())));
} catch (Exception e) {
e.printStackTrace();
}
}//if
}
}

That was quick!:)

Now you should create a debug runtime configuration which can be done at “Run » Debug Configurations…“. Here you select “Android Application” and press “New“. Enter a name and select the “DialANumber” project. Launching the default Activity might be a good idea as we not know what the f*** it means right now.:)So just hit “Apply” and “Debug” and the program will be started in the Android simulator. To give you a clue how that looks like here are some screen shots:

Android Development Demo: “DialANumber”

Android Development Demo: “DialANumber”

As there is an EditField for the phone number which can be modified you can also enter a new phone number in there. An example of this comes here:

Android Development Demo: “DialANumber”

Android Development Demo: “DialANumber”

As everyone can see we are able to dial the number in two different ways:

  1. Hit the “Dial!” button next to the input field.
  2. Hit the “green dial” button on the bottom of the device.

Here is an illustration of this:

Android Development Demo: “DialANumber” - How to write your own very first Android program - Made Simple!

That’s it. Pretty easy, huh?:)

I am looking forward to find some time in the near future to pimp this example a bit. I am thinking of a number field filter adding (which looks a bit confusing in the API documentation) or something else. Suggestions are welcome!

In case someone isreally lazyinterested you can download the whole example project here:dialanumber.zip.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值