RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
Android切近实战(二)

上篇文章我们的那个登陆界面比较简陋,这篇文章稍微对这块进行了修改,如下

创新互联是专业的界首网站建设公司,界首接单;提供成都网站建设、网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行界首网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

Android切近实战(二)

看到那个文本框中的图片和文本框中的文字了吗


            

图片就是上面代码中的drawableLeft这个属性来设置,而文字则是通过android:hint来设置

其文字是设置在string.xml中

Android切近实战(二)

内容如下



    个人理财
    用户登录
    用户名:
    密    码:
    重复密码:
    角色:
    管理员
    登  录
    请输入您的用户名
    请输入您的密码
    请再次输入您的密码
    #fff000
    取消
    确定
    用户注册
    系统参数
    数据备份
    欢迎您,
    显示密码
    提示
    用户名不能为空!
    密码不能为空!
    重复密码和密码不一致!
    保存成功!
    密码长度必须介于6到15位!

OK,通过设置android:hint="@string/hintInputUserNo"就可以实现水印效果。

OK,今天来看一下登录成功的界面以及功能

Android切近实战(二)

首先是登录成功后,会将登录用户的用户名传递给上面的界面。

public class index extends Activity {
    TextView labUser;
    ImageButton imgBtnUserRegister;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.index);
        labUser = (TextView) this.findViewById(R.id.labUser);
        imgBtnUserRegister=(ImageButton)this.findViewById(R.id.imgBtnUserRegister);
        imgBtnUserRegister.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(index.this,userregister.class);
                startActivityForResult(intent, 0);
            }
        });
        Init();
    }
    private void Init() {
        Bundle bundle = getIntent().getExtras();
        String userNo = bundle.getString("userNo");
        labUser.setText(userNo);
    }
}

在初始化Init方法中,接收到登陆界面传递过来的userNo,显示在TextView中。Index界面的代码如下



    
        
        
        
    
    
    
        
            
            
        
        
            
            
        
        
            
            
        
    

本界面采用TabelLayout布局。我们点击imgBtnUserRegister按钮,跳转到用户注册界面

Android切近实战(二)

Intent intent = new Intent();
                intent.setClass(index.this,userregister.class);
                startActivityForResult(intent, 0);

界面UI代码如下



    
        
            
            
        
        
            
            
        
        
            
            
        
        
            
            
        
    
    
        
        
    

点击确定按钮,注册用户。

btnSure.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                String userName = txtUserName.getText().toString().trim();
                String pwd = txtPwd.getText().toString().trim();
                String rePwd = txtRePwd.getText().toString().trim();
                String isAdmin = chkRole.isChecked() ? "1" : "0";
                if (!CheckInput(userName, pwd, rePwd))
                    return;
                SoapObject response = GetServerResponse(userName, pwd, isAdmin);
                Boolean isSuccess = Boolean.valueOf(response.getProperty(
                        "IsSuccess").toString());
                if (isSuccess) {
                    ShowMessage(R.string.SaveSuccess);
                } else {
                    String errorMsg = response.getProperty("ErrorMessage")
                            .toString();
                    new AlertDialog.Builder(owner).setTitle(
                            R.string.WarningMsgTitle).setMessage(errorMsg)
                            .setIcon(R.drawable.info).setPositiveButton("确定",
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(
                                                DialogInterface dialoginterface,
                                                int i) {
                                            txtUserName.setText("");
                                            txtPwd.setText("");
                                            txtRePwd.setText("");
                                            chkRole.setChecked(false);
                                        }
                                    }).show();
                }
            }
        });

首先先Check输入的用户名和密码等

private Boolean CheckInput(String userName, String pwd, String rePwd) {
        if (userName == null || userName.equals("")) {
            new AlertDialog.Builder(this).setTitle(R.string.WarningMsgTitle)
                    .setMessage(R.string.UserNoIsEmpty)
                    .setIcon(R.drawable.info).setPositiveButton("确定",
                            new DialogInterface.OnClickListener() {
                                public void onClick(
                                        DialogInterface dialoginterface, int i) {
                                    txtUserName.requestFocus();
                                }
                            }).show();
            return false;
        } 此处省略部分代码

如下都是一些Check。

Android切近实战(二)Android切近实战(二)

Android切近实战(二)Android切近实战(二)

Android切近实战(二)

OK,到此一个用户就注册成功了。我们先看一下.net webservice端。

namespace GRLC.WebService
{
    /// 
    /// UserInfoMng 的摘要说明
    /// 
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
    // [System.Web.Script.Services.ScriptService]
    public class UserInfoMng : System.Web.Services.WebService
    {
        [WebMethod]
        public CommonResponse UserInfoAdd(UserEntity userInfo)
        {
            return UserInfoBiz.GetInstance().AddUserInfo(userInfo);
        }
    }
}

下面是Biz层代码

namespace GRLC.Biz
{
    public class UserInfoBiz
    {
        static UserInfoBiz userInfoBiz = new UserInfoBiz();
        private UserInfoBiz()
        { }
        public static UserInfoBiz GetInstance()
        {
            return userInfoBiz;
        }
        const string moduleName = "UserInfoModule";
        private string GetMessageByName(string msgName)
        {
            return CommonFunction.GetMessageByModuleAndName(moduleName, msgName);
        }
        private string AddFaild
        {
            get
            {
                return this.GetMessageByName("AddFailed");
            }
        }
        private string UserHasExists
        {
            get
            {
                return this.GetMessageByName("UserHasExists");
            }
        }
        public CommonResponse AddUserInfo(UserEntity userEntity)
        {
            if (UserInfoMngDAL.GetInstance().IsUserInfoExists(userEntity.UseNo))
            {
                return new CommonResponse() { IsSuccess = false, ErrorMessage = UserHasExists };
            }
            User user = new User();
            user.UseNo = userEntity.UseNo;
            user.Pwd = Cryptor.Encrypt(userEntity.Pwd);
            user.IsAdmin = userEntity.IsAdmin;
            int suc = UserInfoMngDAL.GetInstance().AddUserInfo(user);
            if (suc > 0)
            {
                return new CommonResponse() { IsSuccess = true };
            }
            return new CommonResponse() { IsSuccess = false, ErrorMessage = AddFaild };
        }
    }
}

其中UserInfoMngDAL的定义如下

namespace GRLC.DAL
{
    public class UserInfoMngDAL
    {
        static UserInfoMngDAL userInfoMngDAL = new UserInfoMngDAL();
        private UserInfoMngDAL()
        { }
        public static UserInfoMngDAL GetInstance()
        {
            return userInfoMngDAL;
        }
        public int AddUserInfo(User user)
        {
            using(BonusEntities bonusEntities=new BonusEntities())
            {
                bonusEntities.User.Add(user);
                return bonusEntities.SaveChanges();
            }
        }
        public bool IsUserInfoExists(string userNo)
        {
            using (BonusEntities bonusEntities = new BonusEntities())
            {
                return bonusEntities.User.Any(u => u.UseNo == userNo);
            }
        }
    }
}

其中UserEntity的定义如下

namespace GRLC.Model.DTO
{
    public class UserEntity
    {
        public string UseNo { get; set; }
        public string Pwd { get; set; }
        public string IsAdmin { get; set; }
    }
}

到时候这个实体需要和Android那边传递的实体保持一致

Android切近实战(二)

Ok,我们接下来看看Android是如何调用的。

private SoapObject GetServerResponse(String userName, String pwd,
            String isAdmin) {
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        UserEntity userEntity = new UserEntity();
        userEntity.setProperty(0, userName);
        userEntity.setProperty(1, pwd);
        userEntity.setProperty(2, isAdmin);
        PropertyInfo pi = new PropertyInfo();
        pi.setName("userInfo");
        pi.setValue(userEntity);
        pi.setType(userEntity.getClass());
        request.addProperty(pi);// 将自定参数加入请求对象中
        SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        soapEnvelope.dotNet = true;
        soapEnvelope.setOutputSoapObject(request);
        HttpTransportSE httpTS = new HttpTransportSE(URL);
        soapEnvelope.bodyOut = httpTS;
        soapEnvelope.setOutputSoapObject(request);// 设置请求参数
        soapEnvelope.addMapping(NAMESPACE, "UserEntity", userEntity.getClass());
        try {
            httpTS.call(SOAP_ACTION, soapEnvelope);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        SoapObject result = null;
        try {
            result = (SoapObject) soapEnvelope.getResponse();
        } catch (SoapFault e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }

OK,调用只要将数据对象传递正确,就不会调用失败。android中定义的UserEntity如下

package bruce.grlc.model.Entity;
import java.util.Hashtable;
import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;
public class UserEntity implements KvmSerializable {
    private String UseNo;
    private String Pwd;
    private String IsAdmin;
    @Override
    public Object getProperty(int arg0) {
        // TODO Auto-generated method stub
        Object property = null;
        switch (arg0) {
        case 0:
            property = this.UseNo;
            break;
        case 1:
            property = this.Pwd;
            break;
        case 2:
            property = this.IsAdmin;
        default:
            break;
        }
        return property;
    }
    @Override
    public int getPropertyCount() {
        // TODO Auto-generated method stub
        return 3;
    }
    @Override
    public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {
        // TODO Auto-generated method stub
        switch (arg0) {
        case 0:
            arg2.type = PropertyInfo.STRING_CLASS;
            arg2.name = "UseNo";
            break;
        case 1:
            arg2.type = PropertyInfo.STRING_CLASS;
            arg2.name = "Pwd";
            break;
        case 2:
            arg2.type = PropertyInfo.STRING_CLASS;
            arg2.name = "IsAdmin";
        default:
            break;
        }
    }
    @Override
    public void setProperty(int arg0, Object arg1) {
        // TODO Auto-generated method stub
        if (arg1 == null)
            return;
        switch (arg0) {
        case 0:
            this.UseNo = arg1.toString();
            break;
        case 1:
            this.Pwd = arg1.toString();
            break;
        case 2:
            this.IsAdmin = arg1.toString();
            break;
        default:
            break;
        }
    }
}

必须继承KvmSerializable这个抽象接口,并实现它的抽象方法。

public abstract interface org.ksoap2.serialization.KvmSerializable {
                                                           
  // Method descriptor #4 (I)Ljava/lang/Object;
  public abstract java.lang.Object getProperty(int arg0);
                                                           
  // Method descriptor #6 ()I
  public abstract int getPropertyCount();
                                                           
  // Method descriptor #8 (ILjava/lang/Object;)V
  public abstract void setProperty(int arg0, java.lang.Object arg1);
                                                           
  // Method descriptor #10 (ILjava/util/Hashtable;Lorg/ksoap2/serialization/PropertyInfo;)V
  public abstract void getPropertyInfo(int arg0, java.util.Hashtable arg1, org.ksoap2.serialization.PropertyInfo arg2);
}

OK,调用就是这么简单。最后,在界面操作的时候,当焦点在文本框中时,总是会弹出输入法,遮挡按钮,为了解决这一问题,需要采取两种方法

第一种,在AndroidManifest.xml中增加android:windowSoftInputMode="adjustResiz



    
    
        
            
                
                
            
        
        
        
        
        
    
    

这句的意思是当输入法出现时,界面自适应,但是有时候界面上的控件比较多时,依然会遮住某些控件。所以如下

第二种解决方案,在触摸屏幕其他地方时,自动关闭输入法

public boolean onTouchEvent(MotionEvent event) {
        InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        im.hideSoftInputFromWindow(getCurrentFocus()
                .getApplicationWindowToken(),
                InputMethodManager.HIDE_NOT_ALWAYS);
        return super.onTouchEvent(event);
    }

OK,本篇到此结束。


文章题目:Android切近实战(二)
本文URL:http://scyingshan.cn/article/goigod.html