在线学习问答(界面开发--登录和注册)
客户端的界面开发包括了三个部分,主要是登录·注册·和界面动画。
注册的代码:
package android.user;
import android.app.Activity;
import android.graphics.Typeface;
import android.widget.TextView;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.db.DBHelper;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
public class Zhuce extends Activity
{
public void showMsg(String value)
{
AlertDialog.Builder dialog=new AlertDialog.Builder(this);
dialog.setTitle("系统提示");
dialog.setMessage(value);
dialog.setPositiveButton("确定",null);
dialog.show();
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.zhuce);
Typeface typeFace = Typeface.createFromAsset(getAssets(),"fonts/n.ttf");
TextView textview = (TextView) findViewById(R.id.n);
textview.setTypeface(typeFace);
Typeface typeFace1 = Typeface.createFromAsset(getAssets(),"fonts/m.ttf");
TextView textview1 = (TextView) findViewById(R.id.a);
textview1.setTypeface(typeFace1);
Typeface typeFace2 = Typeface.createFromAsset(getAssets(),"fonts/m.ttf");
TextView textview2 = (TextView) findViewById(R.id.b);
textview2.setTypeface(typeFace2);
Typeface typeFace3 = Typeface.createFromAsset(getAssets(),"fonts/m.ttf");
TextView textview3 = (TextView) findViewById(R.id.c);
textview3.setTypeface(typeFace3);
Typeface typeFace4 = Typeface.createFromAsset(getAssets(),"fonts/m.ttf");
TextView textview4 = (TextView) findViewById(R.id.Button01);
textview4.setTypeface(typeFace4);
Typeface typeFace5 = Typeface.createFromAsset(getAssets(),"fonts/m.ttf");
TextView textview5 = (TextView) findViewById(R.id.Button02);
textview5.setTypeface(typeFace5);
Button ok=(Button)findViewById(R.id.Button02);
ok.setOnClickListener(new OnClickListener()
{
public void onClick(View v){
Intent intent=new Intent(Zhuce.this,Denglu.class);
startActivity(intent);
}
});
Button yes=(Button)findViewById(R.id.Button01);
yes.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
EditText username=(EditText)findViewById(R.id.EditText01);
EditText password1=(EditText)findViewById(R.id.EditText02);
EditText password2=(EditText)findViewById(R.id.EditText03);
if(username.getText().length()==0)
{
showMsg("请输入登录名称!");
}
else if(password1.getText().length()==0)
showMsg("请输入登录密码!");
else if(!password2.getText().toString().equals(password1.getText().toString()))
showMsg("两次输入的密码不一致!");
else
{
try {
DBHelper dh = new DBHelper(null, null, null, 0);
SQLiteDatabase myschool = dh.getReadableDatabase();
System.out.print("注册--数据库创建成功!");
ContentValues cv = new ContentValues();
cv.put("name", username.getText().toString());
cv.put("pwd", password1.getText().toString());
long jg=myschool.insertOrThrow("users", null, cv);
int jg1= 0;
if(jg1>0)
{
showMsg("注册成功!");
Intent intent=new Intent(Zhuce.this,Denglu.class);
startActivity(intent);
}
else
showMsg("注册失败!");
} catch (Exception e) {
showMsg(e.getMessage());
}
}
}
}
);
}
}
登录的代码:
package android.user;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.db.DBHelper;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Denglu extends Activity
{
DBHelper DBhelp;
SQLiteDatabase myschool;
public void showMsg(String value)
{
AlertDialog.Builder dialog=new AlertDialog.Builder(this);
dialog.setTitle("系统提示");
dialog.setMessage(value);
dialog.setPositiveButton("确定",null);
dialog.show();
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.denglu);
Typeface typeFace = Typeface.createFromAsset(getAssets(),"fonts/n.ttf");
TextView textview = (TextView) findViewById(R.id.a);
textview.setTypeface(typeFace);
Typeface typeFace1 = Typeface.createFromAsset(getAssets(),"fonts/m.ttf");
TextView textview1 = (TextView) findViewById(R.id.b);
textview1.setTypeface(typeFace1);
Typeface typeFace2 = Typeface.createFromAsset(getAssets(),"fonts/m.ttf");
TextView textview2 = (TextView) findViewById(R.id.c);
textview2.setTypeface(typeFace2);
Typeface typeFace3 = Typeface.createFromAsset(getAssets(),"fonts/m.ttf");
TextView textview3 = (TextView) findViewById(R.id.Button01);
textview3.setTypeface(typeFace3);
Button btn=(Button)findViewById(R.id.Button01);
btn.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{ HttpClient client = new DefaultHttpClient();
String uri = "http://192.168.1.103:8080/maiji/begin.du?LoginName=tom&LoginPassword=123" ;
HttpGet get = new HttpGet(uri);
EditText nameText=(EditText)findViewById(R.id.EditText01);
EditText pwdText=(EditText)findViewById(R.id.EditText02);
if(nameText.getText().length()==0 || pwdText.getText().length()==0)
{
showMsg("登录名称和登录密码必须填写!");
}
else
{
try
{
DBhelp=new DBHelper(null, null, null, 0) ;
myschool=DBhelp.getReadableDatabase();
System.out.print("登录--数据库创建成功!");
Cursor reader=myschool.query("users", new String[]{"name","pwd"}, "name=? and pwd=?", new String[]{nameText.getText().toString(),pwdText.getText().toString()}, null, null, null);
if(reader.getCount()>0)
{
String name=reader.getString(reader.getColumnIndex("name"));
String pwd=reader.getString(reader.getColumnIndex("pwd"));
showMsg("登录成功");
Intent intent=new Intent(Denglu.this,CheckAction.class);
startActivity(intent);
}
else
{
showMsg("登录失败");
}
}
catch(Exception e)
{
showMsg(e.getMessage());
}
}
}
});
}
}

浙公网安备 33010602011771号