Domanda

How to get status of Checkbox in TextView while using AlertDialog.

Still i am trying to show Checked status of CheckBox but not getting any value neither True nor False.

Please see below screenshot,

enter image description here

UploadActivity.java code:

TextView tv1;
CheckBox chkIos;

   @Override
    public Dialog onCreateDialog(int id) {

        AlertDialog dialogDetails = null;
        switch (id) {
        case DIALOG_LOGIN:
            LayoutInflater inflater = LayoutInflater.from(this);
            View dialogview = inflater.inflate(R.layout.dialog_layout, null);
            AlertDialog.Builder dialogbuilder = new AlertDialog.Builder(this);
            dialogbuilder.setTitle("Image Information");
            dialogbuilder.setView(dialogview);
            dialogDetails = dialogbuilder.create();                 
            break;  
        }
        return dialogDetails;
    }

    @Override
    public void onPrepareDialog(int id, Dialog dialog) {
        switch (id) {
        case DIALOG_LOGIN:
        final AlertDialog alertDialog = (AlertDialog) dialog;
            Button loginbutton = (Button) alertDialog
                    .findViewById(R.id.btn_login);
            Button cancelbutton = (Button) alertDialog
                    .findViewById(R.id.btn_cancel);
            tv1 = (TextView) alertDialog
                    .findViewById(R.id.textView1);
            chkIos = (CheckBox) alertDialog
                    .findViewById(R.id.chkOption1);


            loginbutton.setOnClickListener(new View.OnClickListener() {
                @Override
                    public void onClick(View v) {
                    SaveData(); 
                    addListenerOnButton();
                }               

                private boolean SaveData() {                        

                    .............               
                    }
                    return true;
                }

                private String getHttpPost(String url,
                        List<NameValuePair> params) {

                    .............
                    }
                    return str.toString();
                }

            });

            cancelbutton.setOnClickListener(new View.OnClickListener() {
                @Override                   
                    public void onClick(View v) {                       

                    ...........
                }
            });          
        }
    }

public void addListenerOnButton() {                 
    chkIos.setOnClickListener(new OnClickListener() {

    @Override
     public void onClick(View v) {

        if (((CheckBox) v).isChecked()) 
            tv1.setText("True");
                else
            tv1.setText("False");
                }
            });
        }
È stato utile?

Soluzione 3

chekBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked==true){
                    status.setText("true");

                    }
                }else if(isChecked==false){
                    status.setText("false");
                }
            }
        });

EDIT just below this line

chkIos = (CheckBox) alertDialog.findViewById(R.id.chkOption1);

use my code Also remove

addListenerOnButton()

this function It'll work for sure. Thanks.

Altri suggerimenti

You're setting the onclickListener when you've clicked on Login.

Move: addListenerOnButton(); outside the loginButton onClick

Yes i agree with @kumar just need to paste kumar's code after this line:

 chkIos = (CheckBox) alertDialog.findViewById(R.id.chkOption1);

and remove this addListenerOnButton() and you are done.. :)

try and implement checkbox's setOnCheckedChangeListener i think it will do

            CheckBox cb = (CheckBox) findViewById(R.id.CheckBox01);
            cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                     // TODO Auto-generated method stub
                     if (buttonView.isChecked()) {
                        //set textview value here as checked

                    }
                     else
                    {
                     //set textview value here as unchecked
                    }

                }
              });
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top