用android完成了一個簡單的註冊功能。

1java

android

main.xml的配置文件編寫:
android

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    >
    app


<LinearLayout

    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <TextView
        
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="用戶名:"
        />
    <EditText
        android:id="@+id/etuser"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="密 碼:"
        />
    <EditText
        android:id="@+id/etpwd"
        
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:password="true"
        />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:text="確認密碼:"
        />
<EditText
           android:id="@+id/etRePassw"
         android:layout_width="0dp"
         android:layout_height="wrap_content"
         android:layout_weight="1"
         android:password="true"/>
 </LinearLayout>
 <LinearLayout
     android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
     <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="選擇性別:"/>
    <RadioGroup
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal"
        >
        <RadioButton
            android:id="@+id/rbMale"
            android:text="男"
            android:checked="true"/>
        <RadioButton
            android:id="@+id/rbfaMale"
            android:text="女"/>
    </RadioGroup>
 </LinearLayout>
 <LinearLayout
     android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
     <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="愛好:"/>
    <RadioGroup
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal"
        >
       <CheckBox
           android:id="@+id/cbRead"
           android:text="讀書"
           android:checked="true"/>
       <CheckBox
           android:id="@+id/cbTourist"
           android:text="旅遊"
           />
       <CheckBox
           android:id="@+id/cbPlayGame"
           android:text="遊戲"
           />
    </RadioGroup>
 </LinearLayout>
 <LinearLayout
     android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
   <Button
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       android:visibility="invisible"
       android:layout_weight="1"
       />
    <Button
        android:id="@+id/btRegister"
         android:text="註冊"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onclick"
        />
   <Button
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       android:visibility="invisible"
       android:layout_weight="1"
       />
     <Button
         android:id="@+id/btEixt"
         android:text="退出"
         android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onclick"
        />
      <Button
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       android:visibility="invisible"
       android:layout_weight="1"
       />
 </LinearLayout>
     <LinearLayout
     android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >   
<Button
        android:text="返回"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|fill_horizontal"
        
        />  
 </LinearLayout>

</LinearLayout>ide




所須要的實體對象:User.javathis

public class User {
    private String userName;
    private String passWord;
    private char sex;
    private String hobby;
 //生成get和set方法
    public User(String userName, String passWord, char sex,
            String hobby) {
        super();
        this.userName = userName;
        this.passWord = passWord;
        this.sex = sex;
        this.hobby = hobby;
    }
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return this.userName+","+this.passWord+","
        +this.sex+","+this.hobby;
    }
}日誌



關鍵實現代碼:MainActivity.javacode

public class MainActivity extends Activity {
    
    //填寫信息的屬性
    private CheckBox cbRead = null;
    private CheckBox cbTourist = null;
    private CheckBox cbPlayGame = null;
    private EditText etUser = null;
    private EditText etPassw = null;
    private EditText etRePassw = null;
    private RadioButton rbMale = null;
    private RadioButton rbfaMale = null;
    
    private static final String TAG = null;   //日誌標誌xml

    }


    //獲取註冊屬性
    private void loginInfor() {
        // TODO Auto-generated method stub
        etUser = (EditText) findViewById(R.id.etuser);
        etPassw = (EditText) findViewById(R.id.etpwd);
        etRePassw = (EditText) findViewById(R.id.etRePassw);
        rbMale =  (RadioButton) findViewById(R.id.rbMale);
        rbfaMale =  (RadioButton) findViewById(R.id.rbfaMale);
        cbRead = (CheckBox) findViewById(R.id.cbRead);
        cbTourist = (CheckBox) findViewById(R.id.cbTourist);
        cbPlayGame = (CheckBox) findViewById(R.id.cbPlayGame);
        Log.d(TAG, "this is a string log  ---- --");
    }對象

    public void onclick(View v){
        switch(v.getId()){
        
        case R.id.btRegister:
            String userName =  etUser.getText().toString();
            if(TextUtils.isEmpty(userName)){
            Toast.makeText(this, "用戶名不能爲空", 2000).show();
            return;
            
            }
            String passW = etPassw.getText().toString();
            if(TextUtils.isEmpty(passW)){
            etPassw.setError("密碼不能爲空");
            return;
            }
            String rePassw = etRePassw.getText().toString();
            if(TextUtils.isEmpty(rePassw)){
            etRePassw.setError("確認密碼不能爲空");
            return;
            }
            if(!passW.equals(rePassw)){
            Toast.makeText(this, "兩次密碼不一致", 2000).show();
            return ;
            }
            char sex ;
            if(rbMale.isChecked()){
            sex = rbMale.getText().charAt(0);
            }else{
            sex = rbfaMale.getText().charAt(0);
            }
            StringBuffer s = new StringBuffer();
            if(cbRead.isChecked()){
            s.append(cbRead.getText().toString()+",");
            }
            if(cbTourist.isChecked()){
              s.append(cbTourist.getText().toString()+",");
              }
            if(cbPlayGame.isChecked()){
              s.append(cbPlayGame.getText().toString()+",");
              }
            User user = new User(userName,passW,sex,s.toString().equals("")?"":s.toString().substring(0, s.length()));
            Toast.makeText(this, "用戶註冊信息:"+user.toString(),100000).show();
                break;
            case R.id.btEixt:
            finish();
            break;
        
        }//switch結束
        
    }//方法結束遊戲

}

相關文章
相關標籤/搜索