`
$会飞的鱼$
  • 浏览: 25593 次
  • 性别: Icon_minigender_1
  • 来自: 嘉兴
社区版块
存档分类

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

阅读更多
      通过几天android的学习,大概也有了一点点门道,今天花了一点时间把远程控制的客户端登陆界面写出来了,当然只有一个外观,还没有添加具体的登陆事件的处理,明天再慢慢来处理吧,先把今天的成果挂上来。
首先是效果图:
[img]

[/img]

二话不说,直接上代码,一会儿解释:
先看一下布局管理的main.xml
引用

[<?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>

其次就是源代码了:

引用

[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();
    }
}


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

相关推荐

Global site tag (gtag.js) - Google Analytics