android示例:一个简单的登陆程序
最近写了个简单的登陆程序,有几点收获:
1.懂得如何在LinearLayout中嵌套LinearLayout,完善布局的行列;
2.用android:layout_weight控制控件的比重;
3.用getText()获取EditText内容;
4.熟悉控件的编写,不用再照着书抄写了=.=
代码如下:
LoginActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; public class LoginActivity extends Activity {
private Button button;
private EditText editText1;
private EditText editText2; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
//加载按钮和文件框等控件
button=(Button)findViewById(R.id.button);
editText1=(EditText)findViewById(R.id.input_account);
editText2=(EditText)findViewById(R.id.input_password); button.setOnClickListener(new OnClickListener(){
public void onClick(View v){
//获取editText的文本内容,并删去空格
String inputAccount=editText1.getText().toString().trim();
String inputPassword=editText2.getText().toString().trim();
//如果账号密码为"123"就跳转活动
if(inputAccount.equals("123")&& inputPassword.equals("123")) {
Toast.makeText(LoginActivity.this, "账号密码正确,正在登陆中", Toast.LENGTH_SHORT).show();
Intent intent=new Intent(LoginActivity.this,MainActivity.class);
startActivity(intent);
}else {
Toast.makeText(LoginActivity.this, "账号或者密码不正确,请重新输入", Toast.LENGTH_SHORT).show(); }
}
}); } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.setting, menu);
return true;
} }
LoginActivity.java
MainActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.setting, menu);
return true;
} }
MainActivity.java
login.xml
<LinearLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".LoginActivity"
android:orientation="vertical"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
> <TextView
android:id="@+id/account"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="帐号:" /> <EditText
android:id="@+id/input_account"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Input your account"
android:layout_weight="1" > </EditText>
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:" />
<EditText
android:id="@+id/input_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Input your password"
android:layout_weight="1" /> </LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"> <Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登陆" /> </LinearLayout>
</LinearLayout>
login.xml
activity_main.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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登陆成功!撒花~" /> </RelativeLayout>
activity_main.xml
在AndroidManifest.xml中注册Activity
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.logindemo"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.logindemo.LoginActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <activity
android:name="com.example.logindemo.MainActivity" >
</activity>
</application> </manifest>
AndroidManifest.xml
运行效果如下:

帐号密码均输入“123”,就可以成功登陆了。。开心^_^
android示例:一个简单的登陆程序的更多相关文章
- 一个简单的JSP程序示例
		<%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ... 
- 利用JSP编程技术实现一个简单的购物车程序
		实验二 JSP编程 一.实验目的1. 掌握JSP指令的使用方法:2. 掌握JSP动作的使用方法:3. 掌握JSP内置对象的使用方法:4. 掌握JavaBean的编程技术及使用方法:5. 掌握JSP ... 
- (原创)如何使用boost.asio写一个简单的通信程序(一)
		boost.asio相信很多人听说过,作为一个跨平台的通信库,它的性能是很出色的,然而它却谈不上好用,里面有很多地方稍不注意就会出错,要正确的用好asio还是需要花一番精力去学习和实践的,本文将通过介 ... 
- [WCF学习笔记] 我的WCF之旅(1):创建一个简单的WCF程序
		近日学习WCF,找了很多资料,终于找到了Artech这个不错的系列.希望能从中有所收获. 本文用于记录在学习和实践WCF过程中遇到的各种基础问题以及解决方法,以供日后回顾翻阅.可能这些问题都很基础,可 ... 
- Flink源码分析 - 剖析一个简单的Flink程序
		本篇文章首发于头条号Flink程序是如何执行的?通过源码来剖析一个简单的Flink程序,欢迎关注头条号和微信公众号"大数据技术和人工智能"(微信搜索bigdata_ai_tech) ... 
- 编写一个简单的C++程序
		编写一个简单的C++程序 每个C++程序都包含一个或多个函数(function),其中一个必须命名为main.操作系统通过调用main来运行C++程序.下面是一个非常简单的main函数,它什么也不干, ... 
- 使用Go开发一个简单的服务器程序
		最近有个小项目,需要一个简单的后台程序来支撑,本来想用Nodejs来做,但是由于本人js一直很菜,并且很讨厌callback,虽然我也很喜欢异步模型,但我一直都觉得JS是反人类的.后台就用了go处理, ... 
- 一个简单的flask程序
		初始化 所有Flask程序都必须创建一个程序实例. 程序实例是Flask类的对象,经常使用下述代码创建: from flask import Flask app = Flask(__name__) F ... 
- 输出多行字符的一个简单JAVA小程序
		public class JAVA { public static void main(String[] args) { System.out.println("-------------- ... 
随机推荐
- 【Spring学习笔记-MVC-12】Spring MVC视图解析器之ResourceBundleViewResolver
			场景 当我们设计程序界面的时候,中国人希望界面是中文,而美国人希望界面是英文. 我们当然希望后台代码不需改变,系统能够通过配置文件配置,来自己觉得是显示中文界面还是英文界面. 这是,Spring mv ... 
- bzoj4153 [Ipsc2015]Familiar Couples
			Description 有n对夫妇,一开始夫妇之间互不认识,若两男或两女成为朋友,称他们为"熟人","熟人"关系具有传递性,即若a熟b且b熟c则a熟c.若两组夫 ... 
- Java堆外内存之六:堆外内存溢出问题排查
			一.堆外内存组成 通常JVM的参数我们会配置 -Xms 堆初始内存 -Xmx 堆最大内存 -XX:+UseG1GC/CMS 垃圾回收器 -XX:+DisableExplicitGC 禁止显示GC -X ... 
- [转]链接中 href='#' 和 href='###' 的区别以及优缺点
			本文来自:http://c.jinhusns.com/bar/t-829 链接中 href='#' 和 href='###' 的区别以及优缺点 上一篇 下一篇近乎_问阳 发表于:2013-09-09 ... 
- 关于attr("checked")的输出
			同样的一段代码,在不同的文件输出不同的答案,好神奇. console.log($("#never").attr("checked")); 一个输出checked ... 
- AWS CLI以及AWS S3 SYNC命令行使用
			1.到AWS的IAM创建用户,并且获取到访问密钥 ID 和私有访问密钥.下载密钥并保存. 2.到http://docs.amazonaws.cn/cli/latest/userguide/instal ... 
- Jquery阻断事件冒泡(转载)
			JQuery阻止事件冒泡 冒泡事件就是点击子节点,会向上触发父节点,祖先节点的点击事件. 我们在平时的开发过程中,肯定会遇到在一个div(这个div可以是元素)包裹一个div的情况,但是呢,在这两个d ... 
- Web API 源码剖析之全局配置
			Web API 源码剖析之全局配置 Web API 均指Asp.net Web API .本节讲述的是基于Web API 系统在寄宿于IIS. 本节主要讲述Web API全局配置.它是如何优雅的实现 ... 
- ORACLE中index的rebuild(转)
			Oracle里大量删除记录后,表和索引里占用的数据块空间并没有释放. table move可以释放已删除记录表占用的数据块空间,整理碎片.如果将表格用move方式整理碎片后,索引将失效,这时需要将索引 ... 
- solr查询空值、null、不存在的字段的方法
			正常情况下我们都是按有值的方式去搜索,但是有时候有一些字段为null,solr中就没有存储进去,我们怎么获取这个字段不存在为条件的搜索结果了,我们只需要在搜索字段前加上负号,如下图 摘要: Solr的 ... 
