android登录界面按钮无法跳转Activity

kobe121 发布于 2016/03/23 21:55
阅读 1K+
收藏 0

【Gopher China万字分享】华为云的Go语言云原生实战经验!>>>

编写了一个登录注册程序,在虚拟设备上运行,按钮无反应。请各位帮我看看代码哪里出现了问题。代码如下:

public class UserLogin extends Activity implements OnClickListener {
private EditText login_username;    //登录用户名
private EditText login_password;    //登录密码
private Button user_login_button;   //登录密码
private TextView user_register_textView;  //注册文本按钮
 
    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    setContentView(R.layout.login);
    initWidget();
 
}
    private void initWidget()
    {
        login_username=(EditText)findViewById(R.id.et_name_login);
        login_password=(EditText)findViewById(R.id.et_password_login);
        user_login_button=(Button)findViewById(R.id.bt_login);
        user_register_textView=(TextView)findViewById(R.id.txt_toregister);
        user_login_button.setOnClickListener(this);
        user_register_textView.setOnClickListener(this);
        login_username.setOnFocusChangeListener(new OnFocusChangeListener()
        {
 
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                // TODO Auto-generated method stub
                if(!hasFocus){
                    String username=login_username.getText().toString().trim();
                    if(username.length()<4){
                        Toast.makeText(UserLogin.this, "用户名不能小于4个字符", Toast.LENGTH_SHORT);
                    }
                }
            }
            
        });
        login_password.setOnFocusChangeListener(new OnFocusChangeListener()
        {
 
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                // TODO Auto-generated method stub
                if(!hasFocus){
                    String password=login_password.getText().toString().trim();
                    if(password.length()<4){
                        Toast.makeText(UserLogin.this, "密码不能小于4个字符", Toast.LENGTH_SHORT);
                    }
                }
            }
            
        });
    }
    
 
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId())
        {
        case R.id.bt_login:
            if(checkEdit())
            {
                login();
            }
            
            break;
        case R.id.txt_toregister:
            Intent intent2=new Intent(UserLogin.this,UserRegister.class);  //跳转到注册界面
            startActivity(intent2);
            break;
        }
    }
    
    private boolean checkEdit(){
     
        if(login_username.getText().toString().trim().equals("")){
            Toast.makeText(UserLogin.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
           
        }else if(login_password.getText().toString().trim().equals("")){
            Toast.makeText(UserLogin.this, "密码不能为空", Toast.LENGTH_SHORT).show();
          
        }else{
           return true;
        }
        return false;
    }
    
    private void login(){
        String httpUrl="http://192.168.1.102:8080/web-test/login.jsp";
        HttpPost httpRequest=new HttpPost(httpUrl);
        List<NameValuePair> params=new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("username",login_username.getText().toString().trim()));
        params.add(new BasicNameValuePair("password",login_password.getText().toString().trim()));
        HttpEntity httpentity = null;
        try {
            httpentity = new UrlEncodedFormEntity(params,"utf8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        httpRequest.setEntity(httpentity);
        HttpClient httpclient=new DefaultHttpClient();
        HttpResponse httpResponse = null;
        try {
            httpResponse = httpclient.execute(httpRequest);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if(httpResponse.getStatusLine().getStatusCode()==200)
        {
            String strResult = null;
            try {
                strResult = EntityUtils.toString(httpResponse.getEntity());
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Toast.makeText(UserLogin.this, strResult, Toast.LENGTH_SHORT).show();
            Intent intent=new Intent(UserLogin.this,Four_Fuctions.class);   //跳转到主界面
            startActivity(intent);
        }
        else
        {
            Toast.makeText(UserLogin.this, "登录失败!", Toast.LENGTH_SHORT).show();
        }
        
    }
}

 

加载中
0
Z
ZIME

要打开的activity在AndroidManifast中注册吗?如果注册了看看AndroidStudio地下报的什么错误


返回顶部
顶部