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="match_parent"
android:background="#E6E6E6"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_head"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:src="@drawable/ic_launcher11"/>
<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/iv_head"
android:layout_margin="10dp"
android:background="#FFFFFF"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/rl_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="账号"/>
<EditText
android:id="@+id/et_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/tv_name"
android:background="@null"/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#E6E6E6"/>
<RelativeLayout
android:id="@+id/rl_userpsd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView
android:id="@+id/tv_psw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="密码"/>
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/tv_psw"
android:inputType="textPassword"
android:background="@null"/>
</RelativeLayout> </LinearLayout>
<Button
android:id="@+id/btn_login"
android:onClick="student"
android:layout_width="match_parent"
android:layout_height="30dip"
android:layout_below="@+id/layout"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:background="#3C8DC4"
android:text="登录"
android:textColor="#FFFFFF"/>
<Button
android:id="@+id/signUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"
android:layout_marginRight="10dp"
android:background="#E6E6E6"
android:textColor="#000000"
android:layout_marginTop="21dp"
android:layout_centerHorizontal="true"
android:layout_below="@+id/btn_login"
android:layout_alignParentRight="true"/> </RelativeLayout>
package com.itcast.test03;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject; import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends Activity {
private EditText et_username;
private EditText et_userPsd; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_username = (EditText)findViewById(R.id.et_number);
et_userPsd = (EditText)findViewById(R.id.et_password);
}
public void student(View view){
String name = et_username.getText().toString();//把EditTextable类型转化为字符串并赋值给name、password
String password = et_userPsd.getText().toString();
if(TextUtils.isEmpty(name)||TextUtils.isEmpty(password)){//判断输入的用户和密码是否有为空的
Toast.makeText(this, "用户名和密码不能为空", 0).show();
Intent intent01 = new Intent(this,StudentMainActivity.class);
startActivity(intent01);
}else{
try{ HttpClient client = new DefaultHttpClient();//获取HettpClient对象
//指定访问地址
String path = "http://10.6.78.213:2016/xampp/sse/index.php/home/Index/server_info";
HttpPost httpPost = new HttpPost(path);//Post方式请求网络
//请求服务器并获取服务器返回的信息
HttpResponse response = client.execute(httpPost);
//获取状态码
int code = response.getStatusLine().getStatusCode();
if(code == 200){
//将输入流转换成字符串
InputStream is = response.getEntity().getContent();
//将字节输入转换成字符输入流
InputStreamReader in = new InputStreamReader(is);
//对字符流对象进行包装
BufferedReader bufferedReader = new BufferedReader(in);
//StringBuilder(String str) 构造一个字符串生成器,并初始化为指定的字符串内容。
StringBuilder builder = new StringBuilder();
for (String s = bufferedReader.readLine(); s != null; s = bufferedReader
.readLine()) {
builder.append(s);
}
Log.i("cat", ">>>>>>" + builder.toString());
String UName;
String UPass;
/*JsonObject 就是常说的 json。是一种重要的数据传输对象。
* 其格式为{"key1":value1,"key2",value2....};key 必须是字符串。
很像map对不对,一个key,一个value。
因为ajax请求不刷新页面,但配合js可以实现局部刷新,因此json常常被用来作为异步请求的返回对象使用。*/
JSONObject jsonObject = new JSONObject(builder.toString());
UName = jsonObject.getString("user_name");
UPass = jsonObject.getString("user_password"); if(UName.equals(name)&&UPass.equals(password))
{
Toast.makeText(this, "用户名密码输入正确", 0).show();
Intent intent = new Intent(this,StudentMainActivity.class);
startActivity(intent);
} else
{
Toast.makeText(this, "用户名密码输入不正确", 0).show();
}
} }catch(Exception e){
e.printStackTrace(); }
}
} }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#E6E6E6"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"> <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="25sp"
android:textColor="#FFFFFF"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="软件学院的通知和公告" /> </RelativeLayout>
</LinearLayout> <GridView
android:id="@+id/gridView"
android:background="#FFFFFF"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center" >
</GridView> </LinearLayout>
1
package com.itcast.test03; import java.util.ArrayList;
import java.util.HashMap; import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView; public class StudentMainActivity extends Activity {
private GridView gridview;
private GridView gridview1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student_main);
gridview = (GridView)findViewById(R.id.gridView);
ArrayList<HashMap<String,Object>> lstImageItem = new ArrayList<HashMap<String,Object>>();
for(int i = 0;i<20;i++){
HashMap<String,Object> map = new HashMap<String, Object>();
if(i==1){
map.put("ItemImage", R.drawable.ic_launcher02);
map.put("ItemText", "实践教学管理系统");
lstImageItem.add(map);
}else if(i==2){
map.put("ItemImage", R.drawable.ic_launcher06);
map.put("ItemText", "毕业设计管理系统");
lstImageItem.add(map);
}else if(i==3){
map.put("ItemImage", R.drawable.ic_launcher02);
map.put("ItemText", "开放实验管理系统");
lstImageItem.add(map);
}else if(i==4){
map.put("ItemImage", R.drawable.ic_launcher03);
map.put("ItemText", "实习实训管理系统");
lstImageItem.add(map);
}else if(i==5){
map.put("ItemImage", R.drawable.ic_launcher05);
map.put("ItemText", "班级事务管理系统");
lstImageItem.add(map);
}else if(i==6){
map.put("ItemImage", R.drawable.ic_launcher06);
map.put("ItemText", "综合实践管理系统");
lstImageItem.add(map);
}else if(i==7){
map.put("ItemImage", R.drawable.ic_launcher04);
map.put("ItemText", "其他事物2管理系统");
lstImageItem.add(map);
}else if(i==8){
map.put("ItemImage", R.drawable.ic_launcher08);
map.put("ItemText", "其他事物3管理系统");
lstImageItem.add(map);
}else if(i==9){
map.put("ItemImage", R.drawable.ic_launcher01);
map.put("ItemText", "其他事物4管理系统");
lstImageItem.add(map);
}else if(i==10){
map.put("ItemImage", R.drawable.ic_launcher06);
map.put("ItemText", "毕业设计管理系统");
lstImageItem.add(map);
}else if(i==11){
map.put("ItemImage", R.drawable.ic_launcher02);
map.put("ItemText", "开放实验管理系统");
lstImageItem.add(map);
}else if(i==12){
map.put("ItemImage", R.drawable.ic_launcher03);
map.put("ItemText", "实习实训管理系统");
lstImageItem.add(map);
}else if(i==13){
map.put("ItemImage", R.drawable.ic_launcher05);
map.put("ItemText", "班级事务管理系统");
lstImageItem.add(map);
}else if(i==14){
map.put("ItemImage", R.drawable.ic_launcher06);
map.put("ItemText", "综合实践管理系统");
lstImageItem.add(map);
}else if(i==15){
map.put("ItemImage", R.drawable.ic_launcher04);
map.put("ItemText", "其他事物2管理系统");
lstImageItem.add(map);
}else if(i==15){
map.put("ItemImage", R.drawable.ic_launcher08);
map.put("ItemText", "其他事物3管理系统");
lstImageItem.add(map);
}else if(i==17){
map.put("ItemImage", R.drawable.ic_launcher01);
map.put("ItemText", "其他事物4管理系统");
lstImageItem.add(map);
}
}
SimpleAdapter saImageItems = new SimpleAdapter(this,lstImageItem,R.layout.next_activity_student_main,new String[]{"ItemImage","ItemText"},new int[] {R.id.ItemImage,R.id.ItemText});
gridview.setAdapter(saImageItems);
gridview.setOnItemClickListener(new ItemClickListener()); } class ItemClickListener implements OnItemClickListener{ @Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
HashMap<String, Object> item = (HashMap<String,Object>)arg0.getItemAtPosition(arg2);
setTitle((String)item.get("ItemText")); } }
@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;
} }
添加访问权限
<uses-permission android:name="android.permission.INTERNET"/>



Android实现登录的更多相关文章
- Android之登录时密码的保护
在很多的Android项目中都需要用户登录.注册.这样的话在开发中做好保护用户密码的工作就显得尤为重要.这里我把自己的密码保护方法记录下来. 这是我建了一个保存密码的文件,以便于检查自己保存密码或者上 ...
- Android SharedPreferences登录记住密码
SharedPreferences是Android中存储简单数据的一个工具类.可以想象它是一个小小的Cookie,它通过用键值对的方式把简单 数据类型(boolean.int.float.long和S ...
- Android微信登录、分享、支付
转载需要著名出处: http://blog.csdn.net/lowprofile_coding/article/details/78004224 之前写过微信登录分享支付第一版: http://bl ...
- Androidの共享登录之方案研究
由于最近公司提到了一个需求是,一个应用登录成功了,另一个自动登录. 绞尽脑汁想了好几天,看起来很容易但是想深点就漏洞百出,有的时候代码都写完了测试都成功了突然发现给一个假设就完全失效. 先前几个同事之 ...
- egret打包android + android微信登录--小结
公司用egret做了款游戏,需要打android包,做安卓端的微信登录,于是乎开始了第一安卓上的打包,正的是一脸懵 首先遇到的问题有如下: 1. egret打安卓包时经常运行不起来, 主要是gradl ...
- android手机登录时遇到“QQ安全登录发现病毒”解决
android手机作为开源系统非常容易感染病毒,有时候我们会经常遇到手机QQ登录时检测到app被感染,一般情况是由手机感染病毒所引起的,安装腾讯管家后只能检测病毒和卸载感染病毒的软件,不能清除病毒.解 ...
- Android之登录那点事
随着互联网的高速发展,一个应用为了保护用户的隐私,通常会通过设置用户名+密码的验证方式保证用户隐私的相对安全,我知道一般网站的登录验证,通常会设置一个二维码,通过验证二维码,防止恶意软件通过机械程序, ...
- android 第三方登录---新浪微博
1.AndroidManiFest.xml设置,这里我只是简单的用授权,获取基本信息,所以只用了这一个 <!--微博--> <!-- 必须注册在微博授权,分享微博时候用到 --> ...
- android线程登录
主入口代码: package com.tp.soft.app; import java.io.IOException; import java.util.HashMap; import java.ut ...
随机推荐
- 开启事务时mybatis返回主键id
先说一下没有注解的 先给出实体类: public class City { private int city_id; private String city_name; public int getC ...
- Elasticsearch——使用_cat查看Elasticsearch状态
Elasticsearch通过使用JSON来作为沟通的数据格式,这对于开发者来说很友好,因为很多程序都支持JSON格式.比如js就不说了,Java也有fastjson,ruby什么的都自带json. ...
- Javascript动画效果(四)
Javascript动画效果(四) 前面我们自己写了一个小小的关于js动画的插件,下面我们来使用之前的框架来完成我们想要的动画效果.我们经常在淘宝网中看到,鼠标经过某一图片时,该图片有从上滚出而又从下 ...
- char导致的验证异常
表的一个字段: Moblie char(15) 对应的mvc代码: @Html.EditorFor(c => c.Mobile) [RegularExpression("^1[3|4 ...
- Android的px、dp和sp
Android的px.dp和sppx: 即像素,1px代表屏幕上一个物理的像素点:偶尔用到px的情况,是需要画1像素表格线或阴影线的时候. dp: 这个是最常用但也最难理解的尺寸单位.它与“像素密度” ...
- 【CTO讲堂】以API为核心的移动应用云大发展时代
摘要:CTO线上讲堂5月20日正式登场,CTO俱乐部首期邀请到APICloud联合创始人兼CTO邹达与C粉之家微信群友一起聊聊如何快速玩转App开发,分享技术人的职场成长. 为了帮助IT从业者职业之路 ...
- 算法实例-C#-快速排序-QuickSort
算法实例 ##排序算法Sort## ### 快速排序QuickSort ### bing搜索结果 http://www.bing.com/knows/search?q=%E5%BF%AB%E9%80% ...
- Exception raised during rendering: java.lang.System.arraycopy([CI[CII)V
最近下载一个新版本的adt-bundle,Android API是20. 把Plain Text控件往布局上面拖时,发现拖不上去,出现了下面的错误: Exception raised during r ...
- EL表达式之sessionScope
EL 全名为Expression Language EL 语法很简单,它最大的特点就是使用上很方便.接下来介绍EL主要的语法结构: ${sessionScope.user.sex} 所有EL都是以 $ ...
- 怎样实现了捕获应用中的日志在android开发中
怎样实现了捕获应用中的日志在android开发中,大家可研究一下. Process mLogcatProc = null; BufferedReader reader = null; try { mL ...