亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我在簡單的 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 關注
  • 145 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號