http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 4681|回复: 1

[第三方对接] 安卓开发学习笔记-实现一个简单的用户登录界面

[复制链接]
发表于 2016-12-29 09:49:25 | 显示全部楼层 |阅读模式
本次我做了一个用户登录的例子,大致实现的就是输入用户名和密码后点击确定按钮后弹出输入的用户名和密码。
Screenshot_2016-12-29-09-31-53-640_com.example.he.png

打开res/layout/activity_main.xml设计视图,从左侧工具栏控件拉进去相应的空间,并给这些控件重新命名。页面上用户名和密码是用TextView控件,用户名和密码框用的是EditText控件,确定是Button控件。如图:

QQ截图20161229093922.png

xml视图里可以看到这些控件的一些属性:
[Java] 纯文本查看 复制代码
 <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="21dp"
        android:text="用户名" />

    <EditText
        android:id="@+id/txtuname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_marginLeft="27dp"
        android:layout_toRightOf="@+id/textView1"
        android:ems="10"
        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/btnOk"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtuname"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="113dp"
        android:text="确定" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtuname"
        android:layout_marginTop="35dp"
        android:layout_toLeftOf="@+id/txtuname"
        android:text="密码" />

    <EditText
        android:id="@+id/txtPwd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignLeft="@+id/txtuname"
        android:ems="10"
        android:inputType="textPassword" />


其中@+id/后面就是我们自定义的控件id,我们可以直接在这个xml文件里面修改。
视图控件布局设置完成之后如图:
QQ截图20161229094353.png

然后打开src目录下的MainActivit.java,在这个类里面写后台代码:
[Java] 纯文本查看 复制代码
package com.example.hellowordtest;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	EditText uname;
	EditText pwd;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		uname=(EditText)this.findViewById(R.id.txtuname);//获取用户名控件实例
		pwd=(EditText)this.findViewById(R.id.txtPwd);//获取密码控件实例
		uname.setText("我的用户名!");//给用户控件自动赋一个值
		Button btn=(Button)this.findViewById(R.id.btnOk);
		btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				String name=uname.getText().toString();//获取用户名
				String pass=pwd.getText().toString();//获取输入的密码
				String msg="用户名:"+name+",密码:"+pass;
				Toast.makeText(MainActivity.this, msg,Toast.LENGTH_LONG).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.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}


onCreate事件,我们从字面意思来看就是创建页面是发生的事件,在这里写我们的代码,我们通过R.id.控件id的方式去获取页面上的控件,然后可以SetText赋值,getText取值。这里用到一个方法给按钮控件添加点击方法,通过setOnClickListener来实现监视事件。在监视事件里通过Toast.makeText().show()方法来弹出提示信息,效果就如上图实现效果。


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-3-28 17:40

© 2014-2021

快速回复 返回顶部 返回列表