赞
踩
首先要搭建好Java Web开发环境,详细的方法请看另一篇博文点击打开链接
效果:
一、布局
布局如上图,为了控制篇幅就省略了,共用两个layout的.xml文件
二、Activity
import包的部分省略了
代码:LoginActivity.java
- public class LoginActivity extends AppCompatActivity {
-
- EditText id, password;
- Button loginBtn, registerBtn;
- Handler handler;
- static int LOGIN_FAILED = 0;
- static int LOGIN_SUCCEEDED = 1;
- Dialog dialog;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- id = (EditText)findViewById(R.id.id);
- password = (EditText)findViewById(R.id.password);
- loginBtn = (Button)findViewById(R.id.loginBtn);
- registerBtn = (Button)findViewById(R.id.registerBtn);
-
- loginBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v){
- if(isConnectingToInternet()){ //检查网络
- if (id.getText().toString().equals(""))
- Toast.makeText(LoginActivity.this, "请输入账号", Toast.LENGTH_SHORT).show();
- else {
- //启动登录Thread
- dialog = new Dialog(LoginActivity.this);
- dialog.setTitle("正在登录,请稍后...");
- dialog.setCancelable(false);
- dialog.show();
- new LoginPostThread(id.getText().toString(),
- password.getText().toString()).start();
- }
- }else{
- Toast.makeText(getApplicationContext(),
- "网络未连接",Toast.LENGTH_SHORT).show();
- }
- }
- });
-
- registerBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- //跳转到注册Activity
- Intent intent = new Intent(LoginActivity.this,
- RegisterActivity.class);
- startActivity(intent);
- }
- });
-
- //Handle,Msg返回成功信息,跳转到其他Activity
- handler = new Handler() {
- public void handleMessage(Message msg) {
- super.handleMessage(msg);
- dialog.dismiss();
- if (msg.what == 111) { // 处理发送线程传回的消息
- if(msg.obj.toString().equals("SUCCEEDED")){
- //跳转
- Toast.makeText(LoginActivity.this, "模拟跳转",
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。