【问题标题】:Save value in switch statement在 switch 语句中保存值
【发布时间】:2018-01-30 07:23:40
【问题描述】:

在列表视图警报对话框的开关盒中选择一个项目后,如何将值保存到文本视图,谢谢之前

    final AlertDialog.Builder builder = new 
    AlertDialog.Builder(FlightActivity.this);
    builder.setTitle("Pilih Kota Asal");
    // add a list
    final String[] asalkota = {"Jakarta", "Medan", "Palembang", "Bandung", 
    "Yogyakarta", "Palu", "Makassar", "Padang", "Surabaya", "Palangkaraya", 
    "Solo", "Denpasar", "Madura"};
    builder.setItems(asalkota, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
                case 0: 
                case 1: 
                case 2: 
                case 3: 
                case 4: 
                case 5:
                case 6:
                case 7:
                case 8:
                case 9:
                case 10:
                case 11:
                case 12:

                    kotaAsalFlight.setText(asalkota.toString());
            }
        }
    });

    AlertDialog dialog = builder.create();
    dialog.show();
   }

【问题讨论】:

    标签: android switch-statement settext getvalue


    【解决方案1】:

    在每种情况下都使用此代码:

    kotaAsalFlight.setText(asalkota[Your Case Number Here].toString()));
    break;
    

    【讨论】:

      【解决方案2】:

      您不需要开关盒。只需像这样覆盖 onClick 方法:

      @Override
      public void onClick(DialogInterface dialog, int which) {
         kotaAsalFlight.setText(asalkota[which]);
      }
      

      【讨论】:

        【解决方案3】:

        更改这一行:kotaAsalFlight.setText(asalkota.toString());

        到:kotaAsalFlight.setText(asalkota[which]);

        或 将 switch case 实现更改为:

        @Override
        public void onClick(DialogInterface dialog, int which) {
           kotaAsalFlight.setText(asalkota[which]);
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-09-21
          • 2016-10-27
          • 2016-08-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多