`
独爱Java
  • 浏览: 32107 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

android学习笔记之二

阅读更多
Android开发笔记之二
一、初识android应用程序
1、创建应用程序helloworld
2、应用程序主要组成
   Android开发环境搭建在上一篇文章中已经阐述了,不再说明,采用eclipse开发工具进行创建应用程序,右键new选择android工程项目。
   操作完毕后查看生成了几个重要的文件、代码包的activity类、resource文件下的layout和values文件夹、AndroidManifest.xml配置文件以及R.java类。
package com.android;

import android.app.Activity;
import android.os.Bundle;

public class HelloWorldActivity extends Activity {
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

Layout文件下放置布局的配置文件
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>

Values文件下放置一些键值对常量参数
String.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, HelloWorldActivity!</string>
    <string name="app_name">HelloWorld</string>
</resources>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".HelloWorldActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

以上这些就是android应用程序的基本组成部分。
二、初识android应用程序组件
   通过用户登录程序来模拟学习,程序起始界面为用户昵称、用户密码的输入文本框和一个登录按钮,输入完相应的值后提交按钮,后台简单模拟用户名密码都正确提示登录成功。
   通过简单登录模拟学习android应用程序开发,代码模拟如下:
Activity类
package com.android;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;
import android.view.View.OnClickListener;

import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * 用户登录UserLoginActivity类
 * 1、创建的UserLoginActivity类要在AndroidManifest.xml文件中申明
 * 2、创建的UserLoginActivity类要继承自Activity类,覆写其onCreate方法
 * 3、创建的UserLoginActivity类可以添加一些控件,比如text、button等等
 */
public class UserLoginActivity extends Activity {

	// 用户昵称控件
	private EditText usnEditText;
	// 登录密码控件
	private EditText pwdEditText;
	// 提交登录控件 
	private Button loginButton;
	// 登录结果提示控件
	private TextView resTextView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		usnEditText = (EditText)findViewById(R.id.username);
		pwdEditText = (EditText)findViewById(R.id.password);
		loginButton = (Button)findViewById(R.id.loginButton);
		
		// 登录结果控件
		resTextView = (TextView)findViewById(R.id.resultTV);
		resTextView.setText("未登录");
		
		// 添加监听事件
		loginButton.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				// 提取文本框中输入的内容
				String username = usnEditText.getText().toString();
				String password = pwdEditText.getText().toString();
				
				// 只是简单模拟登录操作
				if (username == null || password == null) {
					resTextView.setText("登录失败");
				} else if (username.equals("admin") && password.equals("123")) {
					resTextView.setText("登录成功");
				} else {
					resTextView.setText("登录失败");
				}
			}
		});
	}
}

Main.xml布局配置文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/username" />
    <EditText
        android:id="@+id/username"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName" >
        <requestFocus />
    </EditText>
    
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/password" />
    <EditText
        android:id="@+id/password"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword" />
    
    <Button
        android:id="@+id/loginButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提交登录" />
    
    <TextView
        android:id="@+id/resultTV"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/result" />

</LinearLayout>

字符串常量参数配置文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">用户登录</string>
    <string name="app_name">用户登录程序模拟</string>
    
    <string name="username">用户昵称</string>
    <string name="password">登录密码</string>
    <string name="loginButton">登录</string>
    <string name="result">登录结果提示处</string>
    
</resources>

AndroidManifest.xml配置文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android"
    android:versionCode="1"
    android:versionName="1.0" >
	
    <uses-sdk android:minSdkVersion="7" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        
        <!-- 应用程序启动的activity类配置 -->
        <activity
            android:label="@string/app_name"
            android:name=".UserLoginActivity" > 
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
    </application>

</manifest>

登录操作结果:
起始界面、点击按钮界面见图
  • 大小: 76.6 KB
  • 大小: 95.2 KB
0
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics