Android入门笔记 - 界面开发 - TextView,Button,EditText,Toast

今天简单介绍一下android中的控件资源,我们从一个登陆界面开始讲起,先贴代码:

准备工作:先在eclipse中创建一个android项目,我的项目名称是demo_login。

(1)在项目文件夹的 res/layout/ 文件目录下打开 activity_main.xml :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:text="用户名:" />

        <EditText
            android:id="@+id/et_username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:text="密    码:" />

        <EditText
            android:id="@+id/et_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="登陆" />
        <Button
            android:id="@+id/btn_quit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="取消"/>
    </LinearLayout>

</LinearLayout>

(2)然后打开项目 src/ 目录下的 MainActivity.java 文件:

package com.example.demo_login;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	private Context mCtx;
	private EditText mUsername;
	private EditText mPassword;
	private Button mLogin;
	private Button mQuit;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mCtx = MainActivity.this;
		mUsername = (EditText) findViewById(R.id.et_username);
		mPassword = (EditText) findViewById(R.id.et_password);
		mLogin = (Button) findViewById(R.id.btn_login);
		mQuit = (Button) findViewById(R.id.btn_quit);

		mLogin.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String username = mUsername.getText().toString();
				String password = mPassword.getText().toString();
				if(username.equals("admin") && password.equals("admin")){
					Toast.makeText(mCtx, "登陆成功", Toast.LENGTH_SHORT).show();
				}
				else{
					Toast.makeText(mCtx, "登陆失败", Toast.LENGTH_SHORT).show();
				}
			}
		});

		mQuit.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

好了,代码就这么多,现在开始讲解:

activity_main.xml 文件是 MainActivity 的布局文件,android采用xml的方式配置控件,讲界面和逻辑分开,让开发更有效率,同时也简化了代码。我们先来看activitt_main.xml

定义TextView:

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:text="用户名:" />

定义EditText:

        <EditText
            android:id="@+id/et_username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

定义Button:

        <Button
            android:id="@+id/btn_login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="登陆" />

LinearLayout:流式布局,主要用来控制控件的排列方式,如果不明白,请看后面章节

TextView: 显示固定文本

EditText:文本输入框

Button:按钮

其中EditText 和 Button 设置了 id 属性,是为了我们在MainActivity中可以找到资源文件,并调用它们。现在看一下怎么调用:

我们看到MainActivity的 onCreate()方法, 程序启动后首先会进入这里:

这里就说明我们的布局文件activity_main.xml 是用来填充MainActivity的,setContentView()方法用来设置Activity的加载的布局文件。

		setContentView(R.layout.activity_main);

我们把两个输入框和两个按钮都设置成了成员变量,并采用findViewById(R.id.___)方法从布局文件中找到它们:

	private EditText mUsername;
	private EditText mPassword;
	private Button mLogin;
	private Button mQuit;
	mUsername = (EditText) findViewById(R.id.et_username);
	mPassword = (EditText) findViewById(R.id.et_password);
	mLogin = (Button) findViewById(R.id.btn_login);
	mQuit = (Button) findViewById(R.id.btn_quit);

然后我们给mLogin 登陆按钮设置了鼠标点击事件,如果点击登陆按钮,就会触发里面的 onClick() 方法,里面的功能很简单,就是从EditText中获取到输入的字符串,然后判断是否等于”admin“,如果是则在屏幕弹出Toast登陆成功。说道这里,Toast是什么东西,其实就是提示性的一个气泡,只要知道怎么使用就好了。

		mLogin.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String username = mUsername.getText().toString();
				String password = mPassword.getText().toString();
				if(username.equals("admin") && password.equals("admin")){
					Toast.makeText(mCtx, "登陆成功", Toast.LENGTH_SHORT).show();
				}
				else{
					Toast.makeText(mCtx, "登陆失败", Toast.LENGTH_SHORT).show();
				}
			}
		});

然后mQuit按钮的点击事件调用了 finish() 方法,就是关闭Activity,就不用讲解了吧。

介绍一下Toast:

Toast是一种比较亲切的提示,作用就是给用户提示信息,只是它显示最快,最小,所以称之为亲切。使用很常见,使用方法:

	Toast.makeText(mCtx, "登陆成功", Toast.LENGTH_SHORT).show();

可能有人会问 mCtx 这个参数是什么意思,简单的说这是Context上下文对象,专门用来关联与Activity或Application相关的上下文(资源),我们看 mCtx的初始化:

	mCtx = MainActivity.this;

我比较喜欢在每个Activity中都创建一个 mCtx 对象,这样使用起来就会很方便,比如说创建Toast或者Adapter时很省事。个人使用Toast 我其实喜欢在Activity中封装一个方法专门用来显示Toast:

	private void showToast(String words){
		Toast.makeText(mCtx, words, Toast.LENGTH_SHORT).show();
	}

这样每次要使用Toast的时候,我只需要调用 showToast("要显示的语句")。 这样显得更加简洁,当然是个人爱好了。

好了,第一次就讲到这里吧,我觉得也没有什么好写的,控件的话主要是知道,会用就可以了,当然也可以自定义控件,在后面的章节再来讨论。

下面附上 Android文档 的 chm文件,如果要查文档,就可以用这个。当然推荐是到google官网上去查咯,被封了,没钱翻墙,就将就用吧!

时间: 2024-08-10 19:19:02

Android入门笔记 - 界面开发 - TextView,Button,EditText,Toast的相关文章

Android入门笔记 - 界面开发 - 帧动画

今天来介绍一下Android中的帧动画,帧动画其实就是一张一张的图片,以一定的顺序播放,然后形成动画. 先来上一张效果图: 图中两个按钮,start开始播放动画, stop结束动画,来看看代码: 1. res/ drawable / frame_animation.xml : <?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schem

Android入门笔记 - 界面开发 - Animation

今天我们来看看Android中的动画效果,实例比较简单: AlphaAnimation:透明度动画 ScaleAnimation:缩放动画 TranslateAnimation:移动位置动画 RotateAnimation:旋转角度动画 先贴代码: 这个实例完全使用代码实现的,当然也可以使用xml文件实现,我们先来看这个实例: package com.example.demo5_03_animation; import android.os.Bundle; import android.anno

Android入门笔记1

按钮事件 ? 演示编辑框.文本显示.按钮事件 布局: ? 布局文件: <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="m

Android入门笔记2——获取传感器列表

? UI界面: ? Xml: <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="match_parent" an

[Android学习笔记]ListView中含有Button导致无法响应onItemClick回调的解决办法

转自:http://www.cnblogs.com/eyu8874521/archive/2012/10/17/2727882.html 问题描述: 当ListView的Item中的控件只是一些展示类控件时(比如TextView),注册ListView的监听setOnItemClickListener之后,当点击Item时候会触发onItemClick回调. 但是,当Item中存在Button(继承于Button)的控件时,onItemClick回调不会被触发. 解决方案: 在Item的布局文件

Android入门笔记 - 数据存储 - SharedPreferences

接下来四篇我们来介绍Android中用于数据存储的四种方式: SharedPreferences Sqlite Files 网络 今天我们先来看一个最简单的:SharedPreferences. 这种数据存储方式是最简单,最轻便,也最实用的,但是只能用来储存基本数据类型.我们来看看怎么使用: 1. res/ layout/ activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/an

Android入门笔记(一)

第一部分,Android开发环境的搭建 1.去http://www.oracle.com/technetwork/java/javase/downloads/index.html下载最新版本jdk并安装,配置环境变量. 2.去http://www.eclipse.org/downloads/下载最新版本eclipse,并安装. 3.下载android sdk并安装,下载地址 http://pan.baidu.com/s/1i3ggwhn 4.下载ADT并安装,下载地址http://pan.bai

Android入门笔记 - 多媒体 - MediaPlayer

今天我们来写一个超级超级简单的播放器,使用到的是android自带的音乐播放器类MediaPlayer,先上一张效果图: 里面只实现了播放器额基本功能,界面就不做了,因为不是重点哈. 代码: 1. layout/ activty_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/

Android学习笔记-传感器开发之利用传感器和Tween开发简易指南针

本次我们学习Android传感器的开发,前面已经介绍过了,tween的使用,所以,我们可以结合传感器与tween动画,开发简易的指南针. 首先先介绍一下传感器的相关知识, 在Android应用程序中使用传感器要依赖于android.hardware.SensorEventListener接口.通过该接口可以监听传感器的各种事件.SensorEventListener接口的代码如下: package android.hardware; public interface SensorEventLis