一:用户登录
(1):用户登录XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg1"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="@+id/set_ip_text_view"
style="@style/FontSize"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_marginLeft="500dp"
android:layout_marginTop="50dp"
android:gravity="center"
android:text="@string/setting_ip"
android:textColor="@color/white" />
<LinearLayout
android:layout_width="500dp"
android:layout_height="wrap_content"
android:layout_marginTop="110dp"
android:background="@drawable/dialog_bg"
android:gravity="top|center_horizontal"
android:orientation="vertical"
android:padding="50dip" >
<EditText
android:id="@+id/account_edit_text"
style="@style/FontSize"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@drawable/username_edit_bg"
android:hint="@string/username_hint"
android:singleLine="true" />
<EditText
android:id="@+id/password_edit_text"
style="@style/FontSize"
android:layout_width="match_parent"
android:layout_height="60dip"
android:layout_marginTop="25dp"
android:background="@drawable/password_edit_bg"
android:hint="@string/password_hint"
android:inputType="textPassword"
android:singleLine="true" />
<Button
android:id="@+id/login_button"
style="@style/FontSize"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_marginBottom="15dp"
android:layout_marginTop="25dp"
android:background="@drawable/btn_blue"
android:text="@string/login"
android:textColor="@color/white" />
<TextView
android:id="@+id/register_text_view"
style="@style/FontSize"
android:layout_width="398dp"
android:layout_height="40dp"
android:layout_gravity="end|center_vertical"
android:layout_marginLeft="20dp"
android:layout_weight="0.08"
android:background="@drawable/blue_textview_bg"
android:gravity="center_vertical|center_horizontal"
android:paddingLeft="5dp"
android:text="@string/register"
android:textColor="@color/blue" />
</LinearLayout>
</LinearLayout>
(2):用户登录Activity
package com.example.sends.myapplication;
import...
/**
* 用户登录
*/
public class UserLoginActivity extends BaseActivity {
private TextView mSetIpTV;
private TextView mRegisterTV;
private EditText mUserNameET;
private EditText mPasswordET;
private Button mLoginBtn;
private RequestThread mThread;
private LoginRequest mRequest;
private ProgressDialog mDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_login);
initView();
setListener();
setText();
mDialog = new ProgressDialog(this);
setRequest();
}
private void setRequest() {
mRequest = new LoginRequest("");
mRequest.setOnResponseEventListener(new OnResponseEventListener() {
@Override
public void onResponse(BaseRequest request, RequestResult result) {
mDialog.dismiss();
if (mRequest.isSuccess()) {
Intent mIntent = new Intent(UserLoginActivity.this,
MainActivity.class);
startActivity(mIntent);
UserLoginActivity.this.finish();
} else {
showAlertDialog(getString(R.string.prompt),
getString(R.string.login_failed));
}
}
});
}
/**
* 启动网络连接线程
*/
private void setThread() {
if (mRequest != null) {
mThread = new RequestThread(this, mApp.getHandler());
mThread.setRequest(mRequest);
mThread.start();
}
}
/**
* 初始化控件
*/
private void initView() {
mSetIpTV = (TextView) findViewById(R.id.set_ip_text_view);
mRegisterTV = (TextView) findViewById(R.id.register_text_view);
mUserNameET = (EditText) findViewById(R.id.account_edit_text);
mPasswordET = (EditText) findViewById(R.id.password_edit_text);
mLoginBtn = (Button) findViewById(R.id.login_button);
}
/**
* 设置EditText的值
*/
private void setText() {
mUserNameET.setText(mApp.getUsername());
}
/**
* 设置监听器
*/
private void setListener() {
mSetIpTV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
IPSetDialog dialog = new IPSetDialog(UserLoginActivity.this);
dialog.show(getFragmentManager(), "dialog");
}
});
mRegisterTV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BaseDialog dialog = new UserRegisterDialog(
UserLoginActivity.this);
dialog.show(getFragmentManager(), "dialog");
}
});
mLoginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
userLoginEntry();
setThread();
}
});
}
/**
* 用户登录的方法
*/
private void userLoginEntry() {
String userName = mUserNameET.getText().toString().trim();
String password = mPasswordET.getText().toString().trim();
if (userName.equals("")) {
showAlertDialog(getString(R.string.prompt),
getString(R.string.username_not_empty));
return;
}
if (password.equals("")) {
showAlertDialog(getString(R.string.prompt),
getString(R.string.password_not_empty));
return;
}
mApp.setUsername(userName);
mRequest.setUserName(userName);
mRequest.setPassword(password);
setLoadingDialog();
}
/**
* 数据的加载
*/
private void setLoadingDialog() {
mDialog.setMessage(getString(R.string.login_wait));
mDialog.setCanceledOnTouchOutside(false);
mDialog.setIndeterminate(false);
mDialog.setCancelable(true);
mDialog.show();
}
/**
* 对话框显示
*/
protected void showAlertDialog(String title, String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(title);
builder.setMessage(message);
builder.setPositiveButton(getString(android.R.string.ok), null);
builder.show();
}
}
二:用户注册
(1):用户注册XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="600dp"
android:layout_height="500dp"
android:background="@drawable/bg3"
android:orientation="vertical"
android:padding="20dp" >
<RelativeLayout
android:layout_width="335dp"
android:layout_height="90dp" >
<TextView
style="@style/FontSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/register"
android:textColor="#00FF7F" />
<Button
android:id="@+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:background="@drawable/dialog_close" />
</RelativeLayout>
<View
android:layout_width="wrap_content"
android:layout_height="1dp"
android:background="@drawable/dialog_line" />
<EditText
android:id="@+id/account_edit_text"
style="@style/FontSize"
android:layout_width="255dp"
android:layout_height="60dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:layout_marginTop="20dp"
android:background="@drawable/username_edit_bg"
android:hint="@string/username_hint"
android:singleLine="true" />
<EditText
android:id="@+id/password_edit_text"
style="@style/FontSize"
android:layout_width="252dp"
android:layout_height="60dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:layout_marginTop="25dp"
android:background="@drawable/password_edit_bg"
android:hint="@string/password_hint"
android:inputType="textPassword"
android:singleLine="true" />
<Button
android:id="@+id/ok_button"
style="@style/FontSize"
android:layout_width="257dp"
android:layout_height="60dip"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:layout_marginTop="25dp"
android:background="@drawable/btn_green"
android:text="@android:string/ok"
android:textColor="@color/white" />
</LinearLayout>
(2):用户注册Activity
package com.example.sends.myapplication;
import ...
/**
* 用户注册
*/
public class UserRegisterDialog extends BaseDialog {
private EditText mUserNameET;
private EditText mPasswordET;
private Button mCloseBtn;
private Button mPostBtn;
private RegisterRequest mRequest;
public UserRegisterDialog(Context mContext) {
super(mContext);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getDialog().getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
return inflater.inflate(R.layout.user_register_dialog, container);
}
@Override
public void onActivityCreated(Bundle arg0) {
super.onActivityCreated(arg0);
setRequest();
initView();
setListener();
}
/**
* 设置请求对象
*
*/
private void setRequest() {
mRequest = new RegisterRequest("");
mRequest.setOnResponseEventListener(new OnResponseEventListener() {
@Override
public void onResponse(BaseRequest request, RequestResult result) {
if (mRequest.isSuccess()) {
showAlertDialog(mContext.getString(R.string.prompt),
mContext.getString(R.string.register_success));
UserRegisterDialog.this.dismiss();
} else {
showAlertDialog(mContext.getString(R.string.prompt),
mContext.getString(R.string.register_failed));
}
}
});
}
/**
* 初始化控件
*/
private void initView() {
mCloseBtn = (Button) getView().findViewById(R.id.close);
mUserNameET = (EditText) getView().findViewById(R.id.account_edit_text);
mPasswordET = (EditText) getView()
.findViewById(R.id.password_edit_text);
mPostBtn = (Button) getView().findViewById(R.id.ok_button);
}
/**
* 设置监听器
*/
private void setListener() {
mCloseBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UserRegisterDialog.this.dismiss();
}
});
mPostBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
userRegisterEnter();
}
});
}
/**
* 用户注册的方法
*/
private void userRegisterEnter() {
String userName = mUserNameET.getText().toString().trim();
String password = mPasswordET.getText().toString().trim();
if (userName.equals("")) {
showAlertDialog(mContext.getString(R.string.prompt),
mContext.getString(R.string.username_not_empty));
return;
}
if (password.equals("")) {
showAlertDialog(mContext.getString(R.string.prompt),
mContext.getString(R.string.password_not_empty));
return;
}
mRequest.setUserName(userName);
mRequest.setPassword(password);
startRequest(mRequest);
}
}