您的位置:首页 > 移动开发 > Android开发

Android 用户注册界面的实现

2015-08-15 23:19 651 查看
本文主要讲解Android中用户注册界面的实现。详细请看代码:

1.MainActivity.java

public class MainActivity extends Activity implements View.OnClickListener{

	private String TAG= "MainActivity";	
	private EditText mPhonenumber;	
	private EditText mUsername;	
	private EditText mPassword;
	private EditText mConfirmpassword;
	
	private ImageView mClear_phonenumber;
	private ImageView mClear_username;
	private ImageView mClear_password;
	private ImageView mClear_confirmpassword;
	
	private Button mRegisterbtn;
	private Button mCancelbtn;

    private String username;
    private String password;
    private String confirmedPassword;
    public String phonenumber;
    public SharedPreferences mUserInfo;

    public XmlSerializer mSerializer;

    private static final String USER_INFO_PATH =
            Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator +
                   "MyUserInfo" + File.separator + "user_info";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
        mUserInfo = getSharedPreferences("user_info", MODE_PRIVATE);
   //     country = mUserInfo.getString(Constant.KEY_COUNTRY, "");
       phonenumber = mUserInfo.getString("phone", "");
       Log.d(TAG, "电话号码:"+phonenumber);

//        RecorderApplication.getRefWatcher(this).watch(this, TAG);
    }

    private void init() {
    	 mPhonenumber = (EditText)findViewById(R.id.phonenumber);
    	 mUsername = (EditText)findViewById(R.id.username);
    	 mPassword = (EditText)findViewById(R.id.password);
    	 mConfirmpassword= (EditText)findViewById(R.id.confirmpassword);
    	 
    	 mClear_phonenumber = (ImageView)findViewById(R.id.iv_clear_phonenumber);
    	 mClear_username = (ImageView)findViewById(R.id.iv_clear_username);
    	 mClear_password = (ImageView)findViewById(R.id.iv_clear_password);
    	 mClear_confirmpassword = (ImageView)findViewById(R.id.iv_clear_confirmpassword);
    	 
    	 mRegisterbtn = (Button)findViewById(R.id.btn_Register);
         mCancelbtn = (Button)findViewById(R.id.btn_RegisterCancel);

         mPhonenumber.setOnClickListener(this);
         mUsername.setOnClickListener(this);
         mPassword.setOnClickListener(this);
         mConfirmpassword.setOnClickListener(this);
         
         mClear_phonenumber.setOnClickListener(this);
         mClear_username.setOnClickListener(this);
         mClear_password.setOnClickListener(this);
         mClear_confirmpassword.setOnClickListener(this);
         
         mRegisterbtn.setOnClickListener(this);
         mCancelbtn.setOnClickListener(this);

        mPhonenumber.setOnFocusChangeListener(mFocusChangeListener);
        mUsername.setOnFocusChangeListener(mFocusChangeListener);
        mPassword.setOnFocusChangeListener(mFocusChangeListener);
        mConfirmpassword.setOnFocusChangeListener(mFocusChangeListener);
    }

    View.OnFocusChangeListener mFocusChangeListener = new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            int id = view.getId();
            switch (id) {
                case R.id.username:
                    mClear_username.setVisibility(hasFocus ? View.VISIBLE : View.GONE);
                    break;
                case R.id.password:
                    mClear_password.setVisibility(hasFocus ? View.VISIBLE : View.GONE);
                    break;
                case R.id.confirmpassword:
                    mClear_confirmpassword.setVisibility(hasFocus ? View.VISIBLE : View.GONE);
                    break;
                case R.id.phonenumber:
                    mClear_phonenumber.setVisibility(hasFocus ? View.VISIBLE : View.GONE);
                    break;
                default:
                    break;
            }
        }
    };

    @SuppressLint("NewApi") 
    public void writeUserXml(String phone) {
    	JSONArray info = null;
        JSONObject jsonObject = new JSONObject();
        File userinfopath = new File(USER_INFO_PATH + File.separator + phone);
        if (!userinfopath.exists() | !userinfopath.isDirectory()) userinfopath.mkdirs();
        File user = new File(userinfopath.getAbsolutePath(), "user.xml");
        InputStream inPublic = null;
        FileOutputStream fos = null;
        String infos = null;
        StringBuilder builder = new StringBuilder(10240);
        try {
            jsonObject.put("phone",phonenumber);
            jsonObject.put("userName", username);
            jsonObject.put("password", password);
            
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
        	fos = new FileOutputStream(user);
			fos.write(jsonObject.toString().getBytes("UTF-8"));
		  
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        finally{
        	  try {
				fos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
     
        mUserInfo.edit().putBoolean("registered", true)
                .putBoolean("uploadUserInfo", true)
                .apply();
        //提交用户信息
        mUserInfo.edit()
                .putString("phone", phonenumber).apply();
       
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View view) {
        username = mUsername.getText().toString().trim();
        password = mPassword.getText().toString().trim();
        confirmedPassword = mConfirmpassword.getText().toString().trim();
        phonenumber = mPhonenumber.getText().toString().trim();

        switch (view.getId()) {
            case R.id.iv_clear_username:
                mUsername.setText("");
                break;
            case R.id.iv_clear_password:
                mPassword.setText("");
                break;
            case R.id.iv_clear_confirmpassword:
                mConfirmpassword.setText("");
                break;
            case R.id.iv_clear_phonenumber:
                mPhonenumber.setText("");
            case R.id.btn_RegisterCancel:
      		  finish();
      		  break;
            case R.id.btn_Register:
                if (password.equals(confirmedPassword) && password != null && password.length() > 5) {
                    writeUserXml(phonenumber);     
                   //这里可以把注册的信息文件上传到服务器,方便登录时下载验证  
                    finish();
                } else if (!password.equals(confirmedPassword)) {
                    mConfirmpassword.setText("");
                 
                } else {
                   // T.showMessage(this, "input the right password");
                }
        }

    }
}


2.布局文件:activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    android:id="@+id/linearLayout_Register"
    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_background">
	
      <TextView
                    android:text="请填写完整以下用户信息"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                     android:layout_marginTop="22dp"
                    android:gravity="left"
                    android:textColor="#ffffff"
                    android:textSize="18sp"/>
	<!-- 第一行 -->
	 <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="46dp"
                android:layout_marginLeft="14dp"
                android:layout_marginRight="14dp"
                android:layout_marginTop="22dp"
                android:orientation="horizontal">

                <TextView
                    android:text="手机号码"
                    android:layout_width="70dip"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:textColor="#ffffff"
                    android:textSize="16sp"/>
                
                 <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/input_bg"
                    android:gravity="end"
                    android:orientation="horizontal" >
                    
                    <EditText
                        android:id="@+id/phonenumber"
                        android:singleLine="true"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="8dp"
                        android:layout_marginRight="8dp"
                        android:background="@null"
                        android:hint="请填写手机号码"
                        android:inputType="phone"
                        android:textColor="#ff353535"
                        android:textSize="14sp" 
                        android:textCursorDrawable="@null"/>
                       
                      <ImageView
                        android:id="@+id/iv_clear_phonenumber"
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:layout_marginRight="10dp"
                        android:layout_marginEnd="10dp"
                        android:scaleType="centerInside"
                        android:layout_centerVertical="true"
                        android:layout_alignParentRight="true"
                        android:layout_alignParentEnd="true"
                        android:visibility="gone"
                        android:background="@drawable/search_clear_pressed"
                        android:contentDescription="@null"
                        />
                   </RelativeLayout>     
            </LinearLayout>

      <!-- 第二行 -->
	 <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="46dp"
                android:layout_marginLeft="14dp"
                android:layout_marginRight="14dp"
                android:layout_marginTop="22dp"
                android:orientation="horizontal">

                <TextView
                    android:text="用户名"
                    android:layout_width="70dip"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:textColor="#ffffff"
                    android:textSize="16sp"/>
                
                 <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/input_bg"
                    android:gravity="end"
                    android:orientation="horizontal" >
                    
                    <EditText
                        android:id="@+id/username"
                        android:singleLine="true"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="8dp"
                        android:layout_marginRight="8dp"
                        android:background="@null"
                        android:hint="请填写姓名"
                        android:textColor="#ff353535"
                        android:textSize="14sp"
                        android:textCursorDrawable="@null" />
                       
                      <ImageView
                        android:id="@+id/iv_clear_username"
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:layout_marginRight="10dp"
                        android:layout_marginEnd="10dp"
                        android:scaleType="centerInside"
                        android:layout_centerVertical="true"
                        android:layout_alignParentRight="true"
                        android:layout_alignParentEnd="true"
                        android:visibility="gone"
                        android:background="@drawable/search_clear_pressed"
                        android:contentDescription="@null"
                        />
                   </RelativeLayout>     
            </LinearLayout>
    
      <!-- 第三行 -->
	    <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="46dp"
                android:layout_marginLeft="14dp"
                android:layout_marginRight="14dp"
                android:layout_marginTop="22dp"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="70dip"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="密 码"
                    android:textColor="#ffffff"
                    android:textSize="16sp"/>
 
                  <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/input_bg">
                    
                    <EditText
                        android:id="@+id/password"
                        android:maxLength="8"
                        android:singleLine="true"
                        android:digits="._0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLIMNOPQRSTUVWXYZ"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="8dp"
                        android:layout_marginRight="8dp"
                        android:background="@null"
                        android:hint="请输入密码"
                        android:inputType="textPassword"
                        android:textColor="#ff353535"
                        android:textSize="14sp"
                        android:textCursorDrawable="@null"/>
                    
               <ImageView
                        android:id="@+id/iv_clear_password"
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:layout_marginRight="10dp"
                        android:layout_marginEnd="10dp"   
                        android:layout_centerVertical="true"
                        android:layout_alignParentRight="true"
                        android:layout_alignParentEnd="true" 
                        android:scaleType="centerInside"
                        android:visibility="gone"
                        android:background="@drawable/search_clear_pressed"
                        android:contentDescription="@null"
                       />
                </RelativeLayout>
            </LinearLayout>
      <!-- 第四行 -->
      
        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="46dp"
                android:layout_marginLeft="14dp"
                android:layout_marginRight="14dp"
                android:layout_marginTop="22dp"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="70dip"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:text="确认密码"
                    android:textColor="#ffffff"
                    android:textSize="16sp"/>
 
                  <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/input_bg">
                    
                    <EditText
                        android:id="@+id/confirmpassword"
                        android:maxLength="8"
                        android:singleLine="true"
                        android:digits="._0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLIMNOPQRSTUVWXYZ"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="8dp"
                        android:layout_marginRight="8dp"
                        android:background="@null"
                        android:hint="请再次输入密码"
                        android:inputType="textPassword"
                        android:textColor="#ff353535"
                        android:textSize="14sp"
                        android:textCursorDrawable="@null"/>
                    
               <ImageView
                        android:id="@+id/iv_clear_confirmpassword"
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:layout_marginRight="10dp"
                        android:layout_marginEnd="10dp"   
                        android:layout_centerVertical="true"
                        android:layout_alignParentRight="true"
                        android:layout_alignParentEnd="true" 
                        android:scaleType="centerInside"
                        android:visibility="gone"
                        android:background="@drawable/search_clear_pressed"
                        android:contentDescription="@null"
                       />
                </RelativeLayout>
            </LinearLayout>
            
	 <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
        android:layout_marginTop="22dp"
        android:gravity="center_horizontal">
        <Button
			android:id="@+id/btn_Register"
			android:layout_width="80dip"
			android:layout_height="40dip"
			android:text="注册"
			>
	   </Button>
	   <Button 
		    android:id="@+id/btn_RegisterCancel" 
		    android:layout_width="80dip"
			android:layout_height="40dip" 
			android:layout_marginLeft="10dip"
			android:text="取消" 
			>
		</Button>      
	
      </LinearLayout>
</LinearLayout>


3.配置文件AndroidManifest.xml

添加权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: