我的第一个Android软件——简单拨号器

本文分享了一个编程新手首次开发Android应用的经历,重点介绍了界面布局设计的重要性,并提供了使用RelativeLayout和LinearLayout实现布局的具体实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我的“二次元phone”拨号器
[img]http://dl2.iteye.com/upload/attachment/0100/9360/c066f2ca-8b5d-31ee-9e47-9ebb20afe7cd.jpg[/img]
它是可以打电话的哦~~~

[img]http://dl2.iteye.com/upload/attachment/0100/9358/da5124d8-0e31-3c1c-9ceb-85cb51b25d1b.jpg[/img]

[img]http://dl2.iteye.com/upload/attachment/0100/9356/a3723f2e-3222-3bc0-845d-6a471da9885d.jpg[/img]


对于一个编程小白来说,今天完成了第一个Android软件让我也是激动了好久~~~~~~

首先,我觉得Android软件的开发,布局很重要,做之前首先想好布局,这样就不会在做界面的时候乱了方寸,之前不知道布局是个什么东西,光一个界面就重做了好几次,这次我把RelativeLayout,LinearLayout(包括vertical和horizontal)的布局结合在了一起,虽然有点麻烦,但是这样会给我提供一个清晰的思路:

[img]http://dl2.iteye.com/upload/attachment/0100/9349/a4922c69-ef90-3aa6-943b-9c51c37b6b63.jpg[/img]

下面是布局的部分关键代码:
<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="fill_parent"
android:background="@drawable/beijing2"
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=".MainActivity" >//relativelayout布局

<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >//vertical布局

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dp"

android:layout_weight="2" >//horizontal布局
<EditText
android:id="@+id/editText1"
android:layout_width="240dp"
android:layout_height="30dp"
android:layout_marginTop="14dp"
android:background="#504d2b"
android:ems="10"
android:focusable="false"
android:gravity="center"
android:hint="请输入电话号码"
android:textColorHint="#ffffff"
android:textColorLink="#000000" >

<requestFocus />
</EditText>

<Button
android:id="@+id/button14"
style="?android:attr/buttonStyleSmall"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="12dp"
android:background="@drawable/buttondelete"
android:onClick="deleteStr" />
说明:要注意每部分的完整性,即<********/>形式
其次,在主函数里面,需要写出一些主要方法,即视图、文本框的获取,按钮文字的获取,意图方法的创建等:
如:public class MainActivity extends Activity {
//定义一个文本框对象
EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获得一个文本框对象
et = (EditText) this.findViewById(R.id.editText1);
//this.findViewById(R.id.editText1)得到的是视图,加(EditText)就可以转型
}
//点击按钮获得内容的方法
public void clickButton(View view){
//在Eclipse中的Android日志窗口中显示按钮值
Log.i("按钮的值",view.getTag().toString());
//获得文本框中的内容
String str = et.getText().toString();
//获得之前点击的数,并将新数进行追加
str += view.getTag().toString();
//设置文本框中的显示内容
et.setText(str);
}

public void deleteStr(View view){
String str = et.getText().toString();
if((str!=null) && !("".equals(str.trim()))){
str = str.substring(0, str.length()-1);
et.setText(str);
}
}
public void callPhone(View view){
String str = et.getText().toString();

if(str != null && !"".equals(str.trim())){
Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+str));
//Uri表示意图的格式
startActivity(intent);
}else{
Toast.makeText(MainActivity.this, "亲,请输入电话号码~~~", Toast.LENGTH_LONG).show();

}
}
注意:每个Activity是一个界面,而且要记得导包和授权;
最后,关键的授权代码为:
<uses-permission android:name = "android.permission.CALL_PHONE" />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值