猿问

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

我正在尝试制作一个具有开关按钮和文本的应用程序,如果您打开开关并按下按钮;文本上显示的数字将加 1。但如果关闭开关,数字将减 1。但是当我运行我的应用程序并按下按钮时,应用程序崩溃...


我在编程方面没有太多经验,我不知道我做错了什么。我只尝试过这段代码。


public class MainActivity extends AppCompatActivity {


@Override

protected 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);


                        }

                    });


                }

        }

    });

}

}


所以这应该像我之前描述的那样表现,但事实并非如此。


海绵宝宝撒
浏览 113回答 3
3回答

慕慕森

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

阿波罗的战车

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

慕哥6287543

您不需要监听器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 来增加或减少 中的值。
随时随地看视频慕课网APP

相关分类

Java
我要回答