您的位置:首页 > 移动开发 > Android开发

从简单的android 登陆应用 ,学习布局,

2008-11-22 14:19 671 查看
下面是一个简单的Android登陆,使用TableLayout 表格布局来实现,



代码如下:

package com.birds.android.login;

import android.app.Activity;

import android.app.AlertDialog;

import android.app.AlertDialog.Builder;

import android.content.DialogInterface;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

public class LoginUI extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.login_layou);

Button yesBtn = (Button) findViewById(R.id.btn1);

yesBtn.setOnClickListener(new ButtonHandler(this));

Button cancelBtn = (Button) findViewById(R.id.btn2);

cancelBtn.setOnClickListener(new ButtonHandler(this));

}

}

class ButtonHandler implements OnClickListener {

private Activity context;

public ButtonHandler(Activity context) {

this.context = context;

}

@Override

public void onClick(View v) {

EditText text1 = (EditText) context.findViewById(R.id.text1);

EditText text2 = (EditText) context.findViewById(R.id.text2);

if (v.getId() == R.id.btn1) {

Builder alertDialog = new AlertDialog.Builder(context);

alertDialog.setPositiveButton("OK",

new DialogInterface.OnClickListener() {

public void onClick(DialogInterface v, int btn) {

v.cancel(); // 关闭当前的对话框.

}

});

alertDialog.setTitle("输入的信息:" + text1.getText() + " 密码;"

+ text2.getText());

alertDialog.show();

} else if (v.getId() == R.id.btn2) {

text1.setText("");

text2.setText("");

}

}

}

布局设置,我们使用 xml的方式,如下:

login_layou.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="100px"
android:layout_height="wrap_content"
android:text="账号名称:"
/>
<EditText
android:id="@+id/text1"
android:layout_width="150px"
android:layout_height="wrap_content"
android:text=""
android:maxLength="15"/>
</TableRow>

<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<TextView
android:layout_width="100px"
android:layout_height="wrap_content"
android:text="帐号密码:"
/>
<EditText
android:id="@+id/text2"
android:layout_width="150px"
android:layout_height="wrap_content"
android:password="true"
android:maxLength="15"/>
</TableRow>

<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn1"
android:layout_width="80px"
android:layout_height="wrap_content"
android:text="@string/yes"/>
<Button
android:id="@+id/btn2"
android:layout_width="80px"
android:layout_height="wrap_content"
android:text="@string/cancel" />
</TableRow>
</TableLayout>

每个xml都有对应的java 代码方法,看起来xml比代码更为清晰。可以参考google android 文档.非常详细。

<TableRow></TableRow> 默认显示为一行。。

string.xml 如下

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, LoginUI</string>
<string name="app_name">登陆</string>
<string name="yes">确定</string>
<string name="cancel">取消</string>
<string name="text1">t1</string>
<string name="text2">t2</string>
<string name="btn1">btn1</string>
<string name="btn2">btn2</string>
</resources>

TableLayout 和 RelativeLayout 组合可以 显示 多种不同 的布局效果。RelativeLayout 相对复杂一点。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: