发现自己好久没写博客了,最近解决的问题也都没有整理出来,该打该打。
代码很好理解,VerificationUser是验证用户名和密码是否正确的,RegistUser是注册用户的,RegistUser没怎么好好写呢,这个也很简单,只需判断数据库中有没有此name,没有就插入就OK,VerificationUser也是,只需查找数据库中是否有此name,并且密码是否正确即可。
代码都做了注释,本人英文不太好,可能注释写的不好,不过能理解意思就行。其中getSoapObject是给request设置参数,此处参数为键值对,键为webservices的参数(注:此处参数顺序和参数名必须一致)。至于webservices地址和命名空间什么的,你弄好了webservices环境就知道了,services.cs文件头部就是,命名空间也是可以改的,方法名就是你的webservices的方法名称。其实这段代码只要拿过来用就行了,此处已经算是封装了result的获取,以后增加需要修改的就是那两个方法的逻辑性了。
strings.xml
style.xml
Login.java
Verification.java
AlertDialogs.java
ProgressDialogs.java
AlertDialogControl.java
BaseTools.java
Path.java
State.java
WebMethodName.java
WebMethodParameter.java
WebMethodControl.java
AlertDialogModel.java
WebMethod.java
现在公司已经不忙了,所以可以自己学一些自己喜欢的东西,这次我想给学校做一个在线播放器,顺便系统的练练手。
忙活了好几天,终于把webservices给配置好了,我是用的是.net webservices使用起来感觉要比java的axis2好用的多(好用100倍),具体如何搭建.netwebservices我想这个我就不具体说了,太简单了,只需要按照网上步骤来就行(在此之前要搭建好IIS,估计这个才是里面比较复杂的),服务器搭建好就可以写代码了,我写了一个简单的验证用户登录代码 。
[代码]java代码:
/// <summary>
/// Verificate the user
/// </summary>
/// <param name="name"></param>
/// <param name="password"></param>
/// <returns></returns>
[WebMethod]
public String VerificationUser(String name,String password) {
String names,passwords;
names = name;
passwords = password;
if(names.Equals("zero") && passwords.Equals("111111")){
return "1";
}else{
return "0";
}
}
/// <summary>
/// regist the user,ensure isn't the same username
/// </summary>
/// <param name="name"></param>
/// <param name="password"></param>
/// <returns></returns>
[WebMethod]
public String RegistUser(String name,String password) {
return "1";
}
下面是android代码部分。因为我用了MVC模式,所以代码类非常多,好处是容易拓展,而且便于重复利用,坏处是,看起来需要有一点逻辑空间。这段代码最后贴,主要代码做一点解释。
loginservices类:主要来和webservices打交道,使用ksoap获取webservices的返回信息,我的代码估计和网上搜到的不太一样,主要是我把webservices地址命名空间和方法放进了一个实体类中,于是result的获取方法就可以重复利用了,不多说,以下是代码:
[代码]java代码:
package com.huicui.music.services;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.util.Log;
import com.huicui.music.util.Path;
import com.huicui.music.utilcontrol.WebMethodControl;
import com.huicui.music.utilmodel.WebMethod;
public class loginServices {
private String TAG = "loginServices";
/**the address of webservices*/
private String serviceUrl;
/**the namespace of webservices*/
private String namespace;
/**the name of method*/
private String methodName;
/**the url of httpclient*/
private String soapAction;
/**the parameter data*/
private SoapObject request;
private WebMethodControl webmethodcontrol;
private WebMethod webmethod;
private Object result;
public loginServices(){
serviceUrl = Path.getServicePATH();
namespace = Path.getNameSpacePATH();
webmethodcontrol = new WebMethodControl();
}
/**
* set the parameter to request
* @param parameter
* @param request
* @return SoapObject
* */
@SuppressWarnings("rawtypes")
private SoapObject getSoapObject(HashMap<String,String> parameter,SoapObject request){
Iterator iter = parameter.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
request.addProperty(entry.getKey().toString().trim(), entry.getValue().toString().trim());
}
return request;
}
/**return result*/
private Object getResult(WebMethod webmethod){
methodName = webmethod.getMethodname();//get the name of method
soapAction = namespace + methodName;
Object result = null;
try {
request = new SoapObject(namespace,methodName);
request = getSoapObject(webmethod.getMethodhashmap(),request);//set request's elements
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);//set the version number
envelope.bodyOut = request;
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(serviceUrl);
ht.debug = true;
// web service请求
ht.call(soapAction, envelope);
// get result
result = (Object)envelope.getResponse();
} catch (Exception ex) {
ex.printStackTrace();
}
return result;
}
/**
* Verification name and password
* @param parameter the list of parameter
* @return true success to login false false to login
* */
public boolean getVerification(ArrayList<String> parameter){
webmethod = webmethodcontrol.getVerificationUserEntity(parameter);//get webmethod object
result = getResult(webmethod);
if(result != null && "1".equals(result.toString()))
return true;
else{
return false;
}
}
/**regist new user*/
public boolean getRegisetUser(ArrayList<String> parameter){
webmethod = webmethodcontrol.getRegistUserEntity(parameter);//get webmethod object
result = getResult(webmethod);
if(result != null && "1".equals(result.toString()))
return true;
else{
return false;
}
}
}
把项目中各文件都说明一下吧,代码不都贴上来了:
Login:UI界面,不多说,我的登录和注册界面是一个activity;
Verification:Login的控制类,里面存放的是字符串验证方法,登陆和注册的调用方法;
loginServices:你们已经知道了;
AlertDialogs,ProgressDialogs:dialog的封装,以便重复利用,方便快捷;
BaseTools:我的一些常用工具,比如Log,Toast,其中log据说可以设置islogable来达到关闭log的功能此处没研究,我的代码必须修改源代码才能关闭log;
Path:存放的静态的路径,比如webservices的路径;State:一些状态,比如dialog的状态;
WebMethodName:返回webservices的方法名;
WebMethodParameter:设置webservices的参数,此处额外重要,参数必须一致;
AlertDialogControl:alertdialog用到了实体类,因为他的参数比较多,所以有一个control方法;WebMethodControl:返回WebMethod对象;
AlertDialogModel,WebMethod:两个实体类。
Login.xml:
[代码]java代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:id="@+id/logintitle"
style="@style/titlebar"
android:layout_alignParentTop="true"
>
<Button
android:layout_width="50dip"
android:layout_height="wrap_content"
android:id="@+id/loginback"
style="@style/loginback"
android:layout_alignParentLeft="true"
android:text="@string/loginback"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/logintopic"
style="@style/logintopic"
android:text="@string/logintopic"
android:layout_centerInParent="true"
/>
<Button
android:layout_width="50dp"
android:layout_height="wrap_content"
android:id="@+id/register"
style="@style/register"
android:text="@string/register"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@style/loginlayout"
android:id="@+id/usernamelayout"
android:layout_below="@+id/logintitle"
>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@style/loginedit"
android:hint="@string/login_name_hint"
android:id="@+id/username"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@style/loginedit"
android:id="@+id/password"
android:hint="@string/login_password_hint"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@style/loginedit"
android:id="@+id/repassword"
android:hint="@string/login_repassword_hint"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/submit"
style="@style/button"
android:text="@string/submit"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/registsubmit"
style="@style/button"
android:text="@string/register"
/>
</LinearLayout>
</RelativeLayout>
[代码]java代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, HuicuimusicActivity!</string>
<string name="app_name">Huicuimusic</string>
<string name="logintopic">荟萃音乐登陆</string>
<string name="register">注册</string>
<string name="submit">登陆</string>
<string name="loginback">返回</string>
<string name="login_name_hint">请输入4到8位用户名</string>
<string name="login_password_hint">请输入6到10位密码</string>
<string name="wite">请等待</string>
<string name="inputerror">输入的用户或密码格式不对,请按要求输入</string>
<string name="inputempty">用户名或密码为空</string>
<string name="login_repassword_hint">请再次输入密码</string>
<string name="registtopic">荟萃音乐注册</string>
<string name="cipher">两次输入的密码不同</string>
<string name="registsuccess">注册成功</string>
</resources>
[代码]java代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="loginlayout">
<item name="android:paddingLeft">20dp</item>
<item name="android:paddingRight">20dp</item>
<item name="android:orientation">vertical</item>
</style>
<style name="logininput">
</style>
<style name="titlebar">
<item name="android:background">#1a7af2</item>
<item name="android:orientation">horizontal</item>
</style>
<style name="loginback">
<item name="android:width">50dip</item>
<item name="android:height">50dip</item>
<item name="android:background">@drawable/login_btnxml</item>
<item name="android:textColor">#ffffff</item>
<item name="android:textSize">18sp</item>
</style>
<style name="logintopic">
<item name="android:height">50dp</item>
<item name="android:gravity">center</item>
<item name="android:textColor">#ffffff</item>
<item name="android:textSize">18sp</item>
</style>
<style name="register">
<item name="android:width">50dp</item>
<item name="android:height">50dp</item>
<item name="android:gravity">center</item>
<item name="android:textColor">#ffffff</item>
<item name="android:background">@drawable/login_btnxml</item>
<item name="android:textSize">18sp</item>
</style>
<style name="loginedit" >
<item name="android:layout_marginTop">20dp</item>
<item name="android:background">#ccc</item>
<item name="android:height">40dp</item>
<item name="android:paddingLeft">5dp</item>
<item name="android:paddingRight">5dp</item>
</style>
<style name="button">
<item name="android:layout_marginTop">20dp</item>
<item name="android:background">@drawable/login_btnxml</item>
<item name="android:textColor">#ffffff</item>
<item name="android:height">40dp</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">20sp</item>
</style>
<!--
<style name="BasicButtonStyle" parent="@android:style/Widget.Button">
<item name="android:gravity">center_vertical|center_horizontal</item>
<item name="android:textColor">#ff00cc</item>
<item name="android:shadowColor">#ff000000</item>
<item name="android:shadowDx">0</item>
<item name="android:shadowDy">-1</item>
<item name="android:shadowRadius">0.2</item>
<item name="android:textSize">20dip</item>
<item name="android:textStyle">italic</item>
<item name="android:background">@drawable/btn_custom</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
<item name="android:width">200dip</item>
<item name="android:padding">20dip</item>
<item name="android:height">60dip</item>
<item name="android:ellipsize">end</item>
</style>-->
<style name="TextStyle1" >
<item name="android:ellipsize">marquee</item>
<item name="android:gravity">center</item>
<item name="android:hint">http://www.baidu.com</item><!-- Text为空时显示的文字提示信息,可通过textColorHint设置提示信息的颜色 -->
<item name="android:textColorHint">#000000</item>
<item name="android:includeFontPadding">false</item><!--设置文本是否包含顶部和底部额外空白,默认为true-->
<item name="android:linksClickable">true</item><!-- 设置链接是否点击连接,即使设置了autoLink -->
<item name="android:autoLink">web</item><!-- 设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接。可选值(none/web/email/phone/map/all) -->
<item name="android:marqueeRepeatLimit">3</item><!-- 在ellipsize指定marquee的情况下,设置重复滚动的次数,当设置为marquee_forever时表示无限次。 -->
<item name="android:ems">10</item>
<item name="android:maxLength">10</item>
<item name="android:lines">2</item><!-- 设置文本的行数,设置两行就显示两行,即使第二行没有数据。 -->
<item name="android:lineSpacingExtra">20dip</item><!-- 设置行间距 -->
<item name="android:lineSpacingMultiplier">1.5</item><!-- 设置行间距的倍数。如”1.2” -->
<item name="android:numeric">integer</item><!-- 如果被设置,该TextView有一个数字输入法,点击效果 -->
<item name="android:password">false</item><!-- 以小点”.”显示文本 -->
<item name="android:scrollHorizontally">true</item>
<item name="android:shadowColor">#ffff34cc</item>
<item name="android:shadowDy">-1</item><!-- 设置阴影纵向坐标开始位置。 -->
<item name="android:shadowRadius">3.0</item><!-- 设置阴影的半径。设置为0.1就变成字体的颜色了,一般设置为3.0的效果比较好。 -->
<!-- <item name="android:textAppearance">@android:attr/textAppearanceButton</item> -->
<item name="android:textScaleX">2.0</item><!-- 设置文字缩放,默认为1.0f。 -->
<item name="android:textStyle">bold|italic</item>
<item name="android:typeface">monospace</item><!-- 设置文本字体,必须是以下常量值之一:normal 0, sans 1, serif 2, monospace(等宽字体) 3] -->
</style>
<style name="EditStyle1" >
<item name="android:capitalize">characters</item><!-- 设置英文字母大写类型。设置如下值:sentences仅第一个字母大写;words每一个单词首字母大小,用空格区分单词;characters每一个英文字母都大写 -->
<item name="android:cursorVisible">true</item><!-- 设定光标为显示/隐藏,默认显示。如果设置false,即使选中了也不显示光标栏。 -->
<item name="android:digits">12345qwer</item><!-- 设置允许输入哪些字符。如“1234567890.+-*/%\n()” -->
<!-- <item name="android:drawableLeft">@drawable/ic_launcher</item>在text的左边输出一个drawable(如图片)。 -->
<item name="android:drawablePadding">20dip</item><!-- 设置text与drawable(图片)的间隔,与drawableLeft、drawableRight、drawableTop、drawableBottom一起使用,可设置为负数,单独使用没有效果。 -->
<item name="android:editable">true</item><!-- 设置是否可编辑。仍然可以获取光标,但是无法输入。但是可以删除 -->
<item name="android:ellipsize">start</item><!-- 设置当文字过长时,该控件该如何显示。有如下值设置:”start”—–省略号显示在开头;”end”——省略号显示在结尾;”middle”—-省略号显示在中间;”marquee” ——以跑马灯的方式显示(动画横向移动) -->
<item name="android:freezesText">true</item><!-- 设置保存文本的内容以及光标的位置。参见:这里。 -->
<item name="android:gravity">center</item><!-- 设置文本位置,如设置成“center”,文本将居中显示。 -->
<item name="android:hint">隐藏</item><!-- Text为空时显示的文字提示信息,可通过textColorHint设置提示信息的颜色 -->
<!--<item name="android:imeOptions">actionDone</item> 探索ing -->
<item name="android:includeFontPadding">true</item><!-- 设置文本是否包含顶部和底部额外空白,默认为true。 -->
<item name="android:inputType">number</item><!-- 设置文本的类型,用于帮助输入法显示合适的键盘类型。有如下值设置:none、text、textCapCharacters字母大小、textCapWords单词首字母大小、textCapSentences仅第一个字母大小、textAutoCorrect、textAutoComplete自动完成、textMultiLine多行输入、textImeMultiLine输入法多行(如果支持)、textNoSuggestions不提示、textEmailAddress电子邮件地址、textEmailSubject邮件主题、textShortMessage短信息(会多一个表情按钮出来,点开如下图:)、textLongMessage长讯息?、textPersonName人名、textPostalAddress地址、textPassword密码、textVisiblePassword可见密码、textWebEditText作为网页表单的文本、textFilte文本筛选过滤、textPhonetic拼音输入、numberSigned有符号数字格式、numberDecimal可带小数点的浮点格式、phone电话号码、datetime时间日期、date日期、time时间 -->
<item name="android:ems">40</item>
<item name="android:shadowColor">#ffcc00aa</item>
<item name="android:shadowDx">2.0</item>
<item name="android:shadowRadius">3.0</item>
<item name="android:textScaleX">1.0</item> <!--设置文字之间间隔,默认为1.0f。参见TextView的截图 -->
<item name="android:textStyle">bold</item><!-- 此处失效,有冲突,奇怪。。。 -->
</style>
<style name="BigTextStyle">
<item name="android:layout_margin">5dp</item>
<item name="android:textColor">#ff9900</item>
<item name="android:textSize">25sp</item>
</style>
</resources>
[代码]java代码:
package com.huicui.music.login;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.huicui.music.R;
import com.huicui.music.login.control.Verification;
import com.huicui.music.util.BaseTools;
import com.huicui.music.util.ProgressDialogs;
import com.huicui.music.util.State;
/**
*@author zero (975804495@qq.com)
*@date 2012-10-19
*/
public class Login extends Activity{
private String TAG = "Login";
private Button loginback,register,submit,registsubmit;
private EditText username,password,repassword;
private TextView logintopic;
private Context context;
private Verification verification;
private boolean test = false;
private BaseTools basetools;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
//init view
initview();
//init listener
initlistener();
}
/**init controls*/
private void initview(){
context = Login.this;
logintopic = (TextView)findViewById(R.id.logintopic);
loginback = (Button)findViewById(R.id.loginback);
register = (Button)findViewById(R.id.register);
registsubmit = (Button)findViewById(R.id.registsubmit);
registsubmit.setVisibility(View.GONE);
username = (EditText)findViewById(R.id.username);
password = (EditText)findViewById(R.id.password);
repassword = (EditText)findViewById(R.id.repassword);
repassword.setVisibility(View.GONE);
submit = (Button)findViewById(R.id.submit);
verification = new Verification();
basetools = new BaseTools();
}
/**init listener*/
private void initlistener(){
loginback.setOnClickListener(new backlistener());
submit.setOnClickListener(new submitlistener());
register.setOnClickListener(new registerlistener());
registsubmit.setOnClickListener(new registsubmitlistener());
}
/**set the listener of back button*/
private class backlistener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
}
/**set the listener of submit button*/
private class submitlistener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String name = username.getText().toString().trim();
String psw = password.getText().toString().trim();
int result = verification.stringVerification(name, psw);//verificate the name and psw
if(result == 0){
//the format is true,then matching the webservices
ProgressDialogs progressdialog = new ProgressDialogs(context,"",context.getResources().getString(R.string.wite));
progressdialog.show();
if(verification.LoginVerification() == true){
progressdialog.handler.obtainMessage(State.MSG_DISMISS).sendToTarget();
//do the action
BaseTools.Log(TAG, "success to login,the name:" + name + ";the psw:" + psw, 3);
}else{
progressdialog.handler.obtainMessage(State.MSG_DISMISS).sendToTarget();
BaseTools.Log(TAG,"false to login,the name:" + name + ";the psw:" + psw,3);
}
}else{
if(name.length() == 0 || psw.length() == 0){
basetools.showTip(context, getResources().getString(R.string.inputempty));
}else{
basetools.showTip(context, getResources().getString(R.string.inputerror));
}
}
}
}
/**regist listener*/
private class registerlistener implements OnClickListener{
@Override
public void onClick(View arg0) {
// transform the state between the register and login
if(register.getText().toString().equals("注册")){
logintopic.setText(R.string.registtopic);
register.setText(R.string.submit);
submit.setVisibility(View.GONE);
repassword.setVisibility(View.VISIBLE);
registsubmit.setVisibility(View.VISIBLE);
}else{
logintopic.setText(R.string.logintopic);
register.setText(R.string.register);
submit.setVisibility(View.VISIBLE);
repassword.setVisibility(View.GONE);
registsubmit.setVisibility(View.GONE);
}
}
}
/**registsubmit listener*/
private class registsubmitlistener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String name = username.getText().toString().trim();
String psw = password.getText().toString().trim();
String repsw = repassword.getText().toString().trim();
int result = verification.stringVerification(name, psw,repsw);//verificate the name and psw
if(result == 0){
//the format is true,then matching the webservices
ProgressDialogs progressdialog = new ProgressDialogs(context,"",context.getResources().getString(R.string.wite));
progressdialog.show();
if(verification.RegistVerification() == true){
progressdialog.handler.obtainMessage(State.MSG_DISMISS).sendToTarget();
//do the action
basetools.showTip(context, getResources().getString(R.string.registsuccess));//print the success information
finish();
BaseTools.Log(TAG, "success to regist,the name:" + name + ";the psw:" + psw, 3);
}else{
progressdialog.handler.obtainMessage(State.MSG_DISMISS).sendToTarget();
BaseTools.Log(TAG,"false to regist,the name:" + name + ";the psw:" + psw,3);
}
}else if (result == 8){
basetools.showTip(context, getResources().getString(R.string.cipher));
}else{
if(name.length() == 0 || psw.length() == 0){
basetools.showTip(context, getResources().getString(R.string.inputempty));
}else{
basetools.showTip(context, getResources().getString(R.string.inputerror));
}
}
}
}
/**Intercept the back button to finish this activity*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
Log.i(TAG,"intercept the back button");
finish();
}
return super.onKeyDown(keyCode, event);
}
}
[代码]java代码:
package com.huicui.music.login.control;
import java.util.ArrayList;
import com.huicui.music.services.loginServices;
public class Verification {
private String name;
private String psw;
private String repsw;
private ArrayList<String> parameter;
private loginServices loginservices;
public Verification(){
parameter = new ArrayList<String>();
loginservices = new loginServices();
}
/**login verification*/
public boolean LoginVerification(){
setParameterArrayList(name,psw);
if(loginservices.getVerification(parameter)){
return true;
}else{
return false;
}
}
/**regist verification*/
public boolean RegistVerification(){
setParameterArrayList(name,psw);
if(loginservices.getRegisetUser(parameter)){
return true;
}else{
return false;
}
}
/**
* set parameter to arraylist
* */
private void setParameterArrayList(String name,String psw){
if(parameter.size() != 0){
parameter.clear();
}
parameter.add(name);
parameter.add(psw);
}
/**
* verification the psw
* @param name
* @param psw
* @return int 0:OK;1:the length is low;2:the length is high;3:the name's format is error;4:the psw is low;5:the psw is high;
* 6:the name's format is error;
* */
public int stringVerification(String name,String psw){
this.name = name.trim();
this.psw = psw.trim();
return (nameAndpsw() == 7)?0:nameAndpsw();
}
/**
* verification the psw
* @param name
* @param psw
* @return int 0:OK;1:the length is low;2:the length is high;3:the name's format is error;4:the psw is low;5:the psw is high;
* 6:the name's format is error;8:the psw and repsw is not the same
* */
public int stringVerification(String name,String psw,String repsw){
this.name = name.trim();
this.psw = psw.trim();
this.repsw = repsw.trim();
if(psw.equals(repsw)){
return (nameAndpsw() == 7)?0:nameAndpsw();
}else{
return 8;//the psw and repsw is not the same
}
}
private int nameAndpsw(){
if(name.length() <4){
return 1;//the length of name is low of 4
}
if(name.length() > 8){
return 2;//the length of name is high of 8
}
if(!name.matches("[0-9A-Za-z_]*")){
return 3;//format is error
}
if(psw.length() <6){
return 4;//the length of psw is low of 6
}
if(psw.length() > 20){
return 5;//the length of psw is high of 20
}
if(!psw.matches("[0-9A-Za-z_]*")){
return 6;//format is error
}
return 7;
}
}
[代码]java代码:
package com.huicui.music.util;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import com.huicui.music.utilmodel.AlertDialogModel;
/**
*@author zero (975804495@qq.com)
*@date 2012-10-23
*/
public class AlertDialogs extends AlertDialog{
private AlertDialogModel alertdialogmodel;
private String TAG = "AlertDialogs";
private ArrayList<String> options;
private ProgressDialogs progressdialog;
public AlertDialogs(Context context,AlertDialogModel alertdialogmodel,ProgressDialogs progressdialog) {
super(context);
// TODO Auto-generated constructor stub
this.alertdialogmodel = alertdialogmodel;
this.progressdialog = progressdialog;
init();
}
/**Initialization*/
public void init()
{
//this.setTitle(alertdialogmodel.getTitle());
this.setMessage(alertdialogmodel.getMsg());
//this.setIcon(R.drawable.ic_launcher);
this.setCancelable(false);//false:con't be dismiss by backbutton
options = alertdialogmodel.getOptions();
for(int i = 0;i < options.size();i++){
if(i == 0)
this.setButton(options.get(i),ocl);
else if(i == 1)
this.setButton2(options.get(i),ocl);
else
this.setButton3(options.get(i),ocl);
}
}
OnClickListener ocl = new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
switch (arg1) {
case Dialog.BUTTON1:
BaseTools.Log(TAG, "the button 1", 3);
progressdialog.dismiss();
dismiss();
break;
case Dialog.BUTTON2:
BaseTools.Log(TAG, "the button 2", 3);
dismiss();
break;
default:break;
}
}
};
}
[代码]java代码:
package com.huicui.music.util;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import com.huicui.music.R;
import com.huicui.music.utilcontrol.AlertDialogControl;
/**
*@author zero (975804495@qq.com)
*@date 2012-10-23
*/
public class ProgressDialogs extends ProgressDialog{
private String title,con;
private String TAG = "ProgressDialogs";
private Context context;
public ProgressDialogs(Context context, int theme)
{
super(context, theme);
// TODO Auto-generated constructor stub
//init(context);
}
public ProgressDialogs(Context context,String title,String con)
{
super(context);
// TODO Auto-generated constructor stub
this.title = title;
this.con = con;
init();
this.context = context;
}
public Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
// TODO Auto-generated method stub
super.handleMessage(msg);
switch(msg.what)
{
case State.MSG_DELETE:
//BaseTools.Log(TAG, msg.obj.toString(), 3);
ProgressDialogs.this.setMessage((CharSequence) msg.obj);
break;
case State.MSG_DISMISS:
BaseTools.Log(TAG, "MSG_DISMISS received,dialog dismiss", 3);
ProgressDialogs.this.dismiss();//use the cancel could call the event of OnCancelListener
break;
default:
break;
}
}
};
/**Initialization*/
public void init()
{
this.setProgressStyle(ProgressDialog.STYLE_SPINNER);
this.setTitle(title);
this.setMessage(con);
this.setIcon(R.drawable.ic_launcher);
this.setIndeterminate(false);
this.setCancelable(false);//set false:con't be dismiss by backbutton
}
/**intercept the back of button*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
BaseTools.Log(TAG, "print return", 1);
//start AlertDialog, prompt whether dismiss the progressdialog
AlertDialogControl alertdialgcontrol = new AlertDialogControl();
AlertDialogs alertdialogs = new AlertDialogs(context,alertdialgcontrol.getAlertDialogClose(),ProgressDialogs.this);
alertdialogs.show();
}
return super.onKeyDown(keyCode, event);
}
}
[代码]java代码:
package com.huicui.music.utilcontrol;
import java.util.ArrayList;
import com.huicui.music.utilmodel.AlertDialogModel;
/**
*@author zero (975804495@qq.com)
*@date 2012-10-23
*/
public class AlertDialogControl {
private ArrayList<String> options;
public AlertDialogControl(){
options = new ArrayList<String>();
}
/**
* get the entity of AlertDialogs and set the data to it
* @return AlertDialogModel
* */
public AlertDialogModel getAlertDialogClose(){
AlertDialogModel alertdialogmodel = new AlertDialogModel();
alertdialogmodel.setTitle("标题");
alertdialogmodel.setMsg("是否结束等待");
options.add("是");
options.add("否");
alertdialogmodel.setOptions(options);
return alertdialogmodel;
}
}
[代码]java代码:
package com.huicui.music.util;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
public class BaseTools {
private static Boolean isLog = true;
private Toast toast = null;
/**
* 显示Toast提示
*
* @param message
*/
public void showTip(Context context,String message) {
if(toast == null){
toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
}else{
toast.cancel();
toast.setText(message);
}
toast.show();
}
/**print Log level 1:Log.e;level 2:Log.d;level 3:Log.i*/
public static void Log(String TAG,String MSG,int level){
if(isLog){
switch(level){
case 1:
Log.e(TAG,MSG);
break;
case 2:
Log.d(TAG,MSG);
break;
case 3:
Log.i(TAG,MSG);
break;
default:
break;
}
}
}
/**
* 判断字符是否为空
* @param text 传入的字符串
* @return true:为空;false:不为空
*/
public static boolean isAlbumNameKong(String text){
if(text.trim().equals("") || text.trim().equals("请输入相册名")){
return true;
}else{
return false;
}
}
/**
* 判断俩字符串是否一样
* @param one:第一个字符串;two:第二个字符串
* @return true:一样;false:不一样
*/
public static boolean TwoISSame(String one,String two){
if(one.trim().equals(two.trim())){
return true;
}else{
return false;
}
}
}
[代码]java代码:
package com.huicui.music.util;
public class Path {
private static String ServicePATH = "http://172.16.131.38:8013/Service.asmx";
private static String NameSpacePATH = "http://com.zero.huicui/";
public static String getServicePATH(){
return ServicePATH;
}
public static String getNameSpacePATH(){
return NameSpacePATH;
}
}
[代码]java代码:
package com.huicui.music.util;
public class State {
public static final int MSG_DISMISS = 0x0000;//dialog Handler what :dismiss
public static final int MSG_DELETE = 0x0001;//dialog Handler what :delete
}
[代码]java代码:
package com.huicui.music.util;
/**
*@author zero (975804495@qq.com)
*@date 2012-10-19
*/
public class WebMethodName {
private static String VerificationUserNAME = "VerificationUser";
private static String RegistUserNAME = "RegistUser";
/**return the name of method that Verification user*/
public static String getVerificationUserNAME(){
return VerificationUserNAME;
}
/**return the name of method that Regist User*/
public static String getRegistUserNAME(){
return RegistUserNAME;
}
}
[代码]java代码:
package com.huicui.music.util;
import java.util.ArrayList;
import java.util.HashMap;
import android.util.Log;
/**
*@author zero (975804495@qq.com)
*@date 2012-10-19
*/
public class WebMethodParameter {
private String TAG = "WebMethodParameter";
private HashMap<String,String> hashmap;
public WebMethodParameter(){}
/**
* set the parameter of VerificationUser,if the webservices have been changed ,this method would be changed
* @param parameter
* @return HashMap
* */
public HashMap<String,String> getVerificationUserParameter(ArrayList<String> parameter){
hashmap = new HashMap<String,String>();
if(parameter == null){
Log.e(TAG,"the parameter is null");
return null;
}
if(parameter.get(0) != null){
hashmap.put("name", parameter.get(0));
}else{
Log.e(TAG,"an element in the parameter is null");
return null;
}
if(parameter.get(1) != null){
hashmap.put("password", parameter.get(1));
}else{
Log.e(TAG,"an element in the parameter is null");
return null;
}
return hashmap;
}
/**
* set the parameter of registuser
* @param parameter
* @return HashMap
* */
public HashMap<String,String> getRegistUserParameter(ArrayList<String> parameter){
hashmap = new HashMap<String,String>();
if(parameter == null){
Log.e(TAG,"the parameter is null");
return null;
}
if(parameter.get(0) != null){
hashmap.put("name", parameter.get(0));
}else{
Log.e(TAG,"an element in the parameter is null");
return null;
}
if(parameter.get(1) != null){
hashmap.put("password", parameter.get(1));
}else{
Log.e(TAG,"an element in the parameter is null");
return null;
}
return hashmap;
}
}
[代码]java代码:
package com.huicui.music.utilcontrol;
import java.util.ArrayList;
import com.huicui.music.util.WebMethodName;
import com.huicui.music.util.WebMethodParameter;
import com.huicui.music.utilmodel.WebMethod;
/**
*@author zero (975804495@qq.com)
*@date 2012-10-19
*/
public class WebMethodControl {
private WebMethod webmethod;
private WebMethodParameter webmethodparameter;
public WebMethodControl(){
webmethodparameter = new WebMethodParameter();
}
/**
* get the entity of webmethod,the parameter must be ordered as the webservices.create the entity
* @param parameter
* @return WebMethod
* */
public WebMethod getVerificationUserEntity(ArrayList<String> parameter){
webmethod = new WebMethod();
webmethod.setMethodname(WebMethodName.getVerificationUserNAME());
webmethod.setMethodhashmap(webmethodparameter.getVerificationUserParameter(parameter));
return webmethod;
}
/**
* get the entity of wenmethod,the parameter must be ordered as the webservices.create the entity
* @param parameter
* @return WebMethod
* */
public WebMethod getRegistUserEntity(ArrayList<String> parameter){
webmethod = new WebMethod();
webmethod.setMethodname(WebMethodName.getRegistUserNAME());
webmethod.setMethodhashmap(webmethodparameter.getRegistUserParameter(parameter));
return webmethod;
}
}
[代码]java代码:
package com.huicui.music.utilmodel;
import java.util.ArrayList;
/**
*@author zero (975804495@qq.com)
*@date 2012-10-23
*/
public class AlertDialogModel {
private String title;
private String msg;
private ArrayList<String> options;
public AlertDialogModel(){}
/**
* get the title of alertdialog
* @return String
* */
public String getTitle() {
return title;
}
/**
* set the title of alertdialog
* @param title
* */
public void setTitle(String title) {
this.title = title;
}
/**
* get the msg of alertdialog
* @return String
* */
public String getMsg() {
return msg;
}
/**
* set the mag of alertdialog
* @param mag
* */
public void setMsg(String msg) {
this.msg = msg;
}
/**
* get the options of alertdialog
* @param ArrayList<String>
* */
public ArrayList<String> getOptions() {
return options;
}
/**
* set the options of alertdialog
* @param ArrayList<String>
* */
public void setOptions(ArrayList<String> options) {
this.options = options;
}
}
[代码]java代码:
package com.huicui.music.utilmodel;
import java.util.HashMap;
/**
*@author zero (975804495@qq.com)
*@date 2012-10-19
*/
public class WebMethod {
private String methodname;
private HashMap<String,String> methodhashmap;
public WebMethod(){}
/**
* get the name of method
* @return String
* */
public String getMethodname() {
return methodname;
}
/**
* set the name of method
* @param methodname the name of method
* */
public void setMethodname(String methodname) {
this.methodname = methodname;
}
/**
* get the hashmap of method that put the key and value for webservices
* @return HashMap
* */
public HashMap<String, String> getMethodhashmap() {
return methodhashmap;
}
/**
* set the hashmap of method,put the key and value for webservices,it's important that the key must be the same of webservices
* @param methodhashmap
* */
public void setMethodhashmap(HashMap<String, String> methodhashmap) {
this.methodhashmap = methodhashmap;
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------
荟萃音乐已经中途流产了,因为突然发现这种级别的应用已经没有什么练手的意义存在,不过上述代码还是可以拿来借鉴的