Android开发心得(二)——android布局管理以及常用组件

通过几天android的学习,大概也有了一点点门道,今天花了一点时间把远程控制的客户端登陆界面写出来了,当然只有一个外观,还没有添加具体的登陆事件的处理,明天再慢慢来处理吧,先把今天的成果挂上来。
[b]首先是效果图:[/b]
[img]
[img]http://dl.iteye.com/upload/attachment/331937/8820a91f-48d6-32e1-8490-4e8c2c841e05.png[/img]
[/img]

二话不说,直接上代码,一会儿解释:
先看一下布局管理的main.xml
[quote]
[<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="IP:"
/>
<EditText
android:id="@+id/IP"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="port:"
/>
<EditText
android:id="@+id/port"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
<Button
android:id="@+id/login_bu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"
/>
<Button
android:id="@+id/exit_bu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="退出"
/>
</LinearLayout>
[/quote]
其次就是源代码了:

[quote]
[package net.yang.android;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class LoginActivity extends Activity {
//定义几个全局的变量
public static final String Setting_Infos="Setting_Infos";
public static final String IP="IP";
public static final String port="port";
private android.widget.EditText IP_field;
private android.widget.EditText port_field;
private android.widget.Button login_bu;
private android.widget.Button exit_bu;
private Button.OnClickListener login_bu_listener;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//找到各个相关组件
IP_field=(EditText)this.findViewById(R.id.IP);
port_field=(EditText)this.findViewById(R.id.port);
login_bu=(Button)this.findViewById(R.id.login_bu);
exit_bu=(Button)this.findViewById(R.id.exit_bu);

//获取一个保存上一次用户输入信息的SharedPreferences对象
android.content.SharedPreferences setting=this.getSharedPreferences(Setting_Infos, 0);
String IP_get=setting.getString(IP, " "); //取得保存的IP
String port_get=setting.getString(port, " "); //取得保存的port
//往输入框中添加值
IP_field.setText(IP_get);
port_field.setText(port_get);

//在按钮上加监听器
login_bu.setOnClickListener(login_bu_listener);
//实现监听方法
login_bu_listener=new OnClickListener(){
public void onClick(View v){
Intent intent=new Intent(LoginActivity.this,WorkActivity.class);
Log.d("D","jumped!!!");
startActivity(intent);
}
};
}

/**
* 当Activity结束时,把上一次的用户信息保存到SharedPreferences对象中去
*/
public void onStop(){
super.onStop();
//获取一个保存上一次用户输入信息的SharedPreferences对象
SharedPreferences setting=this.getSharedPreferences(Setting_Infos, 0);
setting.edit()
.putString(IP,IP_field.getText().toString() )
.putString(port, port_field.getText().toString())
.commit();
}
}
[/quote]

这个登录界面开发是android里面比较基础的东西,很类似于swing开发,要有一个好看的布局,关键是布局管理器的选择和熟练使用,我这里使用了最简单的LinearLayout,其他的还有FrameLayout,RelativeLayout,TableLayout 等等。

[quote][/quote]
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值