2014-02-17 100 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

酷男,很高興幫助你,有樂趣編碼 –