复选框未注册为已选中

时间:2014-02-17 18:08:44

标签: android checkbox

我正在做一个项目,我有一个复选框。 但是,在选中复选框并继续进行时。 复选框仍然注册为未选中状态。 有人会有任何解决这个问题的方法吗?

这是我的代码:

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._-]+@[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"
        />

1 个答案:

答案 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._-]+@[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的新实例。您希望在创建期间创建一个实例,并在单击按钮时使用该实例