首先,在布局页面一顿乱画,把名字密码和登陆按钮贴上去,背景图布局什么的都不说了,直接贴下面代码可能为引发一些图片找不到之类的错误,敬请留意
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/login"
android:id="@+id/main">
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#f00"
android:src="@drawable/logo"
android:contentDescription="@null"/>
<RelativeLayout
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/imageView1" >
<ImageView
android:id="@+id/imageView2"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginTop="50dp"
android:layout_marginLeft="20dp"
android:src="@drawable/loginicon1"
android:contentDescription="@null"/>
<EditText
android:id="@+id/editText1"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_toRightOf ="@+id/imageView2"
android:layout_alignBottom ="@+id/imageView2"
android:ems="10"
android:hint="@null">
<requestFocus />
</EditText>
<ImageView
android:id="@+id/imageView3"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignRight="@+id/imageView2"
android:layout_below="@+id/editText1"
android:layout_marginTop="30dp"
android:layout_marginLeft="20dp"
android:src="@drawable/loginicon2"
android:contentDescription="@null"/>
<EditText
android:id="@+id/editText2"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_toRightOf ="@+id/imageView3"
android:layout_alignBottom ="@+id/imageView3"
android:ems="10"
android:hint="@null"
android:inputType="textPassword">
<requestFocus />
</EditText>
<TextView android:id="@+id/mresult"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></TextView>
<Spinner android:id="@+id/Spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView3"
android:layout_marginTop="30dp"
></Spinner>
<Button
android:id="@+id/button1"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_below="@+id/Spinner"
android:text="@string/submit"
android:layout_marginTop="30dp"
android:background="#09c863"
android:layout_centerHorizontal ="true"
/>
</RelativeLayout>
</RelativeLayout>
直接写activity逻辑,登陆按钮的点击事件,捉取到登陆名和密码传到请求的服务器,成功后把登陆信息写到SharedPreferences中,用以判断下次登陆后判断
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//w为了能在主线程访问服务器,必须通过一下代码降低安卓版本到2.3以下
String strVer=GetSystemVersion();
strVer=strVer.substring(0,3).trim();
float fv=Float.valueOf(strVer);
if(fv>2.3)
{
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork() // 这里可以替换为detectAll() 就包括了磁盘读写和网络I/O
.penaltyLog() //打印logcat,当然也可以定位到dropbox,通过文件保存相应的log
.build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects() //探测SQLite数据库操作
.penaltyLog() //打印logcat
.penaltyDeath()
.build());
}
spinner = (Spinner) findViewById(R.id.Spinner);
mresult= (TextView) findViewById(R.id.mresult);
adapter = ArrayAdapter.createFromResource(this, R.array.plantes, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new SpinnerXMLSelectedListener());
//提交按钮事件
btnSub = (Button) findViewById(R.id.button1);
btnSub.setOnClickListener(new SubmitData());
StaffNo = (EditText) findViewById(R.id.editText1);
Psd = (EditText) findViewById(R.id.editText2);
}
///获取android版本
public static String GetSystemVersion()
{
return android.os.Build.VERSION.RELEASE;
}
</pre><pre name="code" class="html"> private class SubmitData implements OnClickListener{
@Override
public void onClick(View v) {
<span style="white-space: pre;"> </span>
<span style="white-space: pre;"> </span>
// TODO Auto-generated method stub
try {
<span style="white-space: pre;"> </span>validate();
<span style="white-space: pre;"> </span>} catch (Exception e) {
<span style="white-space: pre;"> </span>// TODO Auto-generated catch block
<span style="white-space: pre;"> </span>e.printStackTrace();
<span style="white-space: pre;"> </span>}
}
}
</pre><pre name="code" class="html">
</pre><pre name="code" class="html"> private void validate() throws Exception {
<span style="white-space:pre"> </span>String StaffValue=StaffNo.getText().toString();
<span style="white-space:pre"> </span>String PswValue=Psd.getText().toString();
if(TextUtils.isEmpty(StaffValue)) {
Toast.makeText(MainActivity.this, "账号不能为空", 1).show();
return ;
}
if(TextUtils.isEmpty(PswValue)) {
Toast.makeText(MainActivity.this, "密码不能为空", 1).show();
return ;
}
//先判断是有已经登陆过
SimpleDateFormat formatter=new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
Date curDate=new Date(System.currentTimeMillis()); //获取当前时间
String strDate= formatter.format(curDate);
SharedPreferences preferences=getSharedPreferences("user",MODE_PRIVATE);
Editor editor=preferences.edit();
//post请求数据
// NameValuePair pair1 = new BasicNameValuePair("StaffNo", StaffValue);
// NameValuePair pair2 = new BasicNameValuePair("Psw", PswValue);
// NameValuePair pair3 = new BasicNameValuePair("curdate", strDate);
// NameValuePair pair4 = new BasicNameValuePair("Type", Type1);
String KeyStr="StaffNo="+StaffValue+"&Psw="+PswValue+"&curdate="+strDate+"&Type="+Type1;
NameValuePair pair1 = new BasicNameValuePair("KeyStr", KeyStr);
List<NameValuePair> pairList = new ArrayList<NameValuePair>();
pairList.add(pair1);
// pairList.add(pair2);
// pairList.add(pair3);
// pairList.add(pair4);
try
{
HttpEntity requestHttpEntity = new UrlEncodedFormEntity(pairList);
// URL使用基本URL即可,其中不需要加参数
HttpPost httpPost = new HttpPost(baseURL);
// 将请求体内容加入请求中
httpPost.setEntity(requestHttpEntity);
// 需要客户端对象来发送请求
HttpClient httpClient = new DefaultHttpClient();
// 发送请求
HttpResponse response = httpClient.execute(httpPost);
// 显示响应
// showResponseResult(response);
if(response.toString()=="1"){
<span style="white-space:pre"> </span> editor.putString("staff", StaffValue); //把登陆信息写到preferences
<span style="white-space:pre"> </span>editor.putString("psw", PswValue);
<span style="white-space:pre"> </span>editor.putString("LastDate", strDate);
editor.putString("Status", "1");
<span style="white-space:pre"> </span>editor.commit();
<span style="white-space:pre"> </span> Intent intent = new Intent();
<span style="white-space:pre"> </span> intent.setClass(MainActivity.this, TextWebWiew.class); //去首页
<span style="white-space:pre"> </span> startActivity(intent);
finish();
}else{
<span style="white-space:pre"> </span> Toast.makeText(MainActivity.this, response.toString(), 1).show();
return ;
<span style="white-space:pre"> </span>
}
}
catch (Exception e)
{
e.printStackTrace();
}
<span style="white-space:pre"> </span>
}