为了账号安全,请及时绑定邮箱和手机立即绑定

我在简单的 android 应用程序中做错了什么?

我在简单的 android 应用程序中做错了什么?

海绵宝宝撒 2022-06-15 16:57:24
我正在尝试制作一个具有开关按钮和文本的应用程序,如果您打开开关并按下按钮;文本上显示的数字将加 1。但如果关闭开关,数字将减 1。但是当我运行我的应用程序并按下按钮时,应用程序崩溃...我在编程方面没有太多经验,我不知道我做错了什么。我只尝试过这段代码。public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    final TextView text = (TextView)findViewById(R.id.textView);    final Button button = (Button)findViewById(R.id.button);    Switch mySwitch = (Switch)findViewById(R.id.mySwitch);    mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {        @Override        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                if (isChecked== true){                    button.setOnClickListener(new View.OnClickListener() {                        @Override                        public void onClick(View v) {                            String text_string = text.getText().toString();                            int text_int = Integer.parseInt(text_string);                            text_int++;                            text.setText(text_int);                        }                    });                }                if (isChecked == false) {                    button.setOnClickListener(new View.OnClickListener() {                        @Override                        public void onClick(View v) {                            String text_string = text.getText().toString();                            int text_int = Integer.parseInt(text_string);                            text_int++;                            text.setText(text_int);                        }                    });                }        }    });}}所以这应该像我之前描述的那样表现,但事实并非如此。
查看完整描述

3 回答

?
慕慕森

TA贡献1856条经验 获得超17个赞

您正在嵌套侦听器,但该逻辑不能按顺序工作。您应该单独声明您的听众。我建议你创建一个布尔值来保存开关和一个按钮监听器的状态。在侦听器中检查是否启用了开关,然后运行您的计算,如果禁用了开关,请执行相同的操作。


    button.setOnClickListener(new View.OnClickListener() {

       @Override

       public void onClick(View v) {

       if(mySwitch.isChecked(){

           String text_string = text.getText().toString();

           int text_int = Integer.parseInt(text_string);

           text_int++;

           text.setText(String.valueOf(text_int));

        } else {

           String text_string = text.getText().toString();

           int text_int = Integer.parseInt(text_string);

           text_int++;

           text.setText(String.valueOf(text_int));

        }

      }

   });


查看完整回答
反对 回复 2022-06-15
?
阿波罗的战车

TA贡献1862条经验 获得超6个赞

您的应用程序崩溃是因为您试图将一个 int 设置为 atextview.setText()并且当您将一个 int 传递给此方法时,它希望它是一个资源 id,并且在您的情况下找不到它,这就是它会抛出ResourceNotFoundException和崩溃的原因。

您应该如下设置文本:

text.setText(String.valueOf(text_int));


查看完整回答
反对 回复 2022-06-15
?
慕哥6287543

TA贡献1831条经验 获得超10个赞

您不需要监听器Switch,但只需要 1 个监听器Button:


final TextView text = (TextView)findViewById(R.id.textView);

final Button button = (Button)findViewById(R.id.button);

final Switch mySwitch = (Switch)findViewById(R.id.mySwitch);


button.setOnClickListener(new View.OnClickListener() {

    @Override

    public void onClick(View v) {

        String text_string = text.getText().toString();

        int text_int = 0;

        try {

            text_int = Integer.parseInt(text_string);

        } catch (NumberFormatException e) {

            e.printStackTrace();

        }

        if (mySwitch.isChecked())

            text_int++;

        else

            text_int--;

        text.setText("" + text_int);

    }

});

每次单击 时Button,在其侦听器中,TextView根据是否选中 Switch 来增加或减少 中的值。


查看完整回答
反对 回复 2022-06-15
  • 3 回答
  • 0 关注
  • 112 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号