2014-02-17 49 views
0

我目前正在执行一个项目,而且我有一个复选框。 但是,通过选中复选框并继续前进。 复选框仍然被注册为未选中状态。 任何人都可以解决这个问题吗?复选框未注册为已检查

这里是我的代码:

final Button button = (Button) findViewById(R.id.loginButton); 
     button.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 

       String email = emailValidate.getText().toString().trim(); 
       String emailPattern = "[a-zA-Z0-9._-][email protected][a-z]+\\.+[a-z]+"; 

       if (emailValidate.getText().toString().trim().matches("") || password.getText().toString().trim().matches("")) { 
        if (emailValidate.getText().toString().trim().matches("") && password.getText().toString().trim().matches("")) { 
         Toast.makeText(getApplicationContext(), "Please enter an E-mail Address and Password", Toast.LENGTH_SHORT).show(); 
        } else if (emailValidate.getText().toString().trim().matches("")) { 
         Toast.makeText(getApplicationContext(), "Please enter an E-mail Address", Toast.LENGTH_SHORT).show(); 
        } else if (password.getText().toString().trim().matches("")) { 
         Toast.makeText(getApplicationContext(), "Please enter a Password", Toast.LENGTH_SHORT).show(); 
        } 
       } 
       else { 
        mWebview.setWebViewClient(new WebViewClient() { 
         @Override 
         public void onPageFinished(WebView view, String url) { 
          String webUrl = mWebview.getUrl(); 

          setContentView(R.layout.student_email); 
          CheckBox cbox1 = (CheckBox)findViewById(R.id.checkbox_store); 
          if (cbox1.isChecked()) 
          { 
           Toast.makeText(getApplicationContext(), "If Statement active", Toast.LENGTH_SHORT).show(); 
           prefEditor.putString("username", emEdit.getText().toString()); 
           prefEditor.putString("password", passEdit.getText().toString()); 
           prefEditor.commit(); 
          } 
          else 
          { 
           Toast.makeText(getApplicationContext(), "Code broke :(", Toast.LENGTH_SHORT).show(); 
          } 
          setContentView(mWebview); 
          mWebview.loadUrl("javascript:(function() { " + "document.getElementById('cred_userid_inputtext').value ='" + emEdit.getText() + "';" + "})()"); 
          mWebview.loadUrl("javascript:(function() { " + "document.getElementById('cred_password_inputtext').value ='" + passEdit.getText() + "';" + "})()"); 
          mWebview.loadUrl("javascript:(function() { " + "document.getElementById('credentials').submit(); return false;" + "})()"); 
         } 
        }); 

        mWebview.loadUrl("http://login.microsoftonline.com"); 
        Toast.makeText(getApplicationContext(), "This works!", Toast.LENGTH_SHORT).show(); 


       } 

      } 
     }); 
    } 

这里的XML:

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

<TextView 
     android:id="@+id/textViewTitle" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true"/> 

<TextView 
     android:id="@+id/textView1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true"/> 

<TextView 
     android:id="@+id/textView" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true"/> 

<TextView 
     android:id="@+id/textViewEmail" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true"/> 

<EditText 
     android:id="@+id/editEmail" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="E-Mail"> 

    <requestFocus /> 

</EditText> 

<TextView 
     android:id="@+id/emailValidate" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true"/> 

<TextView 
     android:id="@+id/textViewPass" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true"/> 

<EditText 
     android:id="@+id/editPass" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:password="true" 
     android:hint="Password"> 

</EditText> 

<CheckBox android:id="@+id/checkbox_store" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Store your data"/> 

<Button 
     android:id="@+id/loginButton" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Login" 
     /> 

+0

是的,只要我点击事件,我会检查它们是否被复选框被选中或者没有被选中,但即使它被选中注册,如果它不是。 –

+0

每点击一下按钮。您正在创建一个未选中复选框的新实例。 –

+0

非常感谢,这正是问题所在!现在完美工作:) –

回答

0
CheckBox cbox1 = (CheckBox)findViewById(R.id.checkbox_store); 
final Button button = (Button) findViewById(R.id.loginButton); 
    button.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 

      String email = emailValidate.getText().toString().trim(); 
      String emailPattern = "[a-zA-Z0-9._-][email protected][a-z]+\\.+[a-z]+"; 

      if (emailValidate.getText().toString().trim().matches("") || password.getText().toString().trim().matches("")) { 
       if (emailValidate.getText().toString().trim().matches("") && password.getText().toString().trim().matches("")) { 
        Toast.makeText(getApplicationContext(), "Please enter an E-mail Address and Password", Toast.LENGTH_SHORT).show(); 
       } else if (emailValidate.getText().toString().trim().matches("")) { 
        Toast.makeText(getApplicationContext(), "Please enter an E-mail Address", Toast.LENGTH_SHORT).show(); 
       } else if (password.getText().toString().trim().matches("")) { 
        Toast.makeText(getApplicationContext(), "Please enter a Password", Toast.LENGTH_SHORT).show(); 
       } 
      } 
      else { 
       mWebview.setWebViewClient(new WebViewClient() { 
        @Override 
        public void onPageFinished(WebView view, String url) { 
         String webUrl = mWebview.getUrl(); 

         setContentView(R.layout.student_email); 
         if (cbox1.isChecked()) 
         { 
          Toast.makeText(getApplicationContext(), "If Statement active", Toast.LENGTH_SHORT).show(); 
          prefEditor.putString("username", emEdit.getText().toString()); 
          prefEditor.putString("password", passEdit.getText().toString()); 
          prefEditor.commit(); 
         } 
         else 
         { 
          Toast.makeText(getApplicationContext(), "Code broke :(", Toast.LENGTH_SHORT).show(); 
         } 
         setContentView(mWebview); 
         mWebview.loadUrl("javascript:(function() { " + "document.getElementById('cred_userid_inputtext').value ='" + emEdit.getText() + "';" + "})()"); 
         mWebview.loadUrl("javascript:(function() { " + "document.getElementById('cred_password_inputtext').value ='" + passEdit.getText() + "';" + "})()"); 
         mWebview.loadUrl("javascript:(function() { " + "document.getElementById('credentials').submit(); return false;" + "})()"); 
        } 
       }); 

       mWebview.loadUrl("http://login.microsoftonline.com"); 
       Toast.makeText(getApplicationContext(), "This works!", Toast.LENGTH_SHORT).show(); 


      } 

     } 
    }); 
} 

更改您的代码上面,你不希望创建一个新的每次按钮被点击时CheckBox的实例。你想在创建过程中创建一个实例,并在点击按钮时使用同一个实例

+0

是的,这确实是问题! 非常感谢:) –

+0

酷男,很高兴帮助你,有乐趣编码 –