验证android studio中是否填写了imagebutton

wvmv3b1j  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(249)

我有3个图像按钮,用户应该选择一个。
如何验证3个图像按钮是否已填充?
我有3个图像按钮:

ImageButton satisfeito = (ImageButton) findViewById(button2);
ImageButton indiferente = (ImageButton) findViewById(button3);
ImageButton nadasatisfeito = (ImageButton) findViewById(button4);

如果单击其中一个,则会显示一条消息:

satisfeito.setOnClickListener(new View.OnClickListener() {
                @Override
        public void onClick(View view) {
             Toast.makeText(getApplicationContext(), "Satisfeito", Toast.LENGTH_SHORT).show();
        }
});

indiferente.setOnClickListener(new View.OnClickListener() {
               @Override
        public void onClick(View view) {
             Toast.makeText(getApplicationContext(), "Indiferente", Toast.LENGTH_SHORT).show();
        }
});

nadasatisfeito.setOnClickListener(new View.OnClickListener() {
               @Override
        public void onClick(View view) {
            Toast.makeText(getApplicationContext(), "Nada Satisfeito", Toast.LENGTH_SHORT).show();
        }
});

但是,我不知道如何查看是否选择了这3个图像按钮。我怎么能做到?

button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if view.isSelected() {
                        view.setSelected(false);
                    }
}

亲切问候,,
里卡多·迪兹

lx0bsm1f

lx0bsm1f1#

在这里使用标志概念

int seleted =0;

satisfeito.setOnClickListener(new View.OnClickListener() {
                @Override
        public void onClick(View view) {
            seleted=1;
             Toast.makeText(getApplicationContext(), "Satisfeito", Toast.LENGTH_SHORT).show();
        }
});

indiferente.setOnClickListener(new View.OnClickListener() {
               @Override
        public void onClick(View view) {
            seleted=2;
             Toast.makeText(getApplicationContext(), "Indiferente", Toast.LENGTH_SHORT).show();
        }
});

nadasatisfeito.setOnClickListener(new View.OnClickListener() {
               @Override
        public void onClick(View view) {
            seleted=3;
            Toast.makeText(getApplicationContext(), "Nada Satisfeito", Toast.LENGTH_SHORT).show();
        }
});
button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //if view.isSelected() {
                        //view.setSelected(false);

                      if(selected == 0){
                     if(selected==1){
                      Toast.makeText(getApplicationContext(), "selected Satisfeito", Toast.LENGTH_SHORT).show();
                    }else if(selected ==2){
             Toast.makeText(getApplicationContext(), "selected Indiferente", Toast.LENGTH_SHORT).show();
             }else if(selected == 3){
            Toast.makeText(getApplicationContext(), "selected Nada Satisfeito", Toast.LENGTH_SHORT).show();
}

                   }else{
            Toast.makeText(getApplicationContext(), "please select one button", Toast.LENGTH_SHORT).show();
            }
                    }
}

相关问题