当前位置:编程学习 > JAVA >>

判断从TextView里取出的字符串是否为空

研究安卓UI,发现从组件TextView里取出的字符串时,需要判断这个字符串是否为空

例如,在用户登录界面,会有用户名的信息,在登录时,系统要获取TextView里的用户名,并判断是否为空

开始时,我是这样判断的


[java]
btn_login.setOnClickListener(new OnClickListener() 
        { 
 
            @Override 
            public void onClick(View v) 
            { 
                ComponentName cn=new ComponentName(MainActivity.this,"com.example.test_intent.login"); 
                String username=userEditText.getText().toString(); 
                if(username==null) 
                {    
                    buliBuilder.setMessage("用户名不能为空!").setPositiveButton("确定", new DialogInte易做图ce.OnClickListener() 
                    { 
                         
                        @Override 
                        public void onClick(DialogInte易做图ce dialog, int which) 
                        { 
                            Toast.makeText(getApplicationContext(), R.string.user_hint, 3000).show(); 
                            // TODO Auto-generated method stub  
                             
                        } 
                    }); 
                    AlertDialog adAlertDialog=buliBuilder.create(); 
                    adAlertDialog.show(); 
                } 
                else  
                { 
                    String password=pswEditText.getText().toString(); 
                    Bundle bundle=new Bundle(); 
                    bundle.putString("username", username); 
                    bundle.putString("password", password); 
                    Intent intent=new Intent(); 
                    intent.setComponent(cn); 
                    intent.putExtras(bundle); 
                    startActivity(intent); 
                // TODO Auto-generated method stub  
                } 
            } 

btn_login.setOnClickListener(new OnClickListener()
  {

   @Override
   public void onClick(View v)
   {
    ComponentName cn=new ComponentName(MainActivity.this,"com.example.test_intent.login");
    String username=userEditText.getText().toString();
    if(username==null)
    { 
     buliBuilder.setMessage("用户名不能为空!").setPositiveButton("确定", new DialogInte易做图ce.OnClickListener()
     {
      
      @Override
      public void onClick(DialogInte易做图ce dialog, int which)
      {
       Toast.makeText(getApplicationContext(), R.string.user_hint, 3000).show();
       // TODO Auto-generated method stub
       
      }
     });
     AlertDialog adAlertDialog=buliBuilder.create();
     adAlertDialog.show();
    }
    else
    {
     String password=pswEditText.getText().toString();
     Bundle bundle=new Bundle();
     bundle.putString("username", username);
     bundle.putString("password", password);
     Intent intent=new Intent();
     intent.setComponent(cn);
     intent.putExtras(bundle);
     startActivity(intent);
    // TODO Auto-generated method stub
    }
   }
直接用username==null(或者username.equals(null))判断,发现这是错的,登录时这个根本起不到判断作用

后来改了一下


[java]
btn_login.setOnClickListener(new OnClickListener() 
        { 
 
   

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,