0

我正在开发一个用户必须选择国家代码的应用程序,我成功地为上述目的创建了一个微调器,如以下链接所示:
为选择国家代码创建微调器 但我在读取所选值时遇到问题在微调器中。

{
               String abc = onCountryPickerClick();//abc is always null 
}  
    public String onCountryPickerClick (){
         ccp.setOnCountryChangeListener(new CountryCodePicker.OnCountryChangeListener() {
             @Override
             public void onCountrySelected() {
                 selected_country_code = ccp.getSelectedCountryCodeWithPlus();
             }

         });
         return  selected_country_code;
    }
4

2 回答 2

1

回调不是同步的。不幸的是,您不能简单地做,String abc = onCountryPickerClick();因为您要返回的是尚未设置的东西。让我们看看你的代码:

ccp.setOnCountryChangeListener(
     new CountryCodePicker.OnCountryChangeListener() {
         @Override
         public void onCountrySelected() {
             selected_country_code = ccp.getSelectedCountryCodeWithPlus();
         }
     });

代码似乎说,当在微调器中选择国家时,您分配selected_country_code. 假设这是用户触发的操作,当您调用 时String abc = onCountryPickerClick();,如何确定用户选择了任何内容?这就是问题所在。您不能确定用户已经选择了该选项并且返回该值是不够的。

您可以通过多种方式解决此问题。例如,您可以继续传播回调:

public void onCountryPickerClick(OnCountryChangeListener listener){
     ccp.setOnCountryChangeListener(listener);
}

// Anywhere you call this
onCountryPickerClick(new CountryCodePicker.OnCountryChangeListener() {
         @Override
         public void onCountrySelected() {
             // Here do whatever you want with the selected country
         }
     });

上述方法与您现在的方法没有太大不同。还有其他选择。您可以使用 java observables,即:

class CountryCodeObservable extends Observable {
  private String value;

  public CountryCodeObservable(String value) {
    this.value = value;
  }

  public void setCountryCode(String countryCode) {
    value = countryCode;
    setChanged();
    notifyObservers(value);
  }
}

public CountryCodeObservable onCountryPickerClick(){
  CountryCodeObservable retValue = new CountryCodeObservable("");

  ccp.setOnCountryChangeListener(
     new CountryCodePicker.OnCountryChangeListener() {
         @Override
         public void onCountrySelected() {
             retValue.setCountryCode(ccp.getSelectedCountryCodeWithPlus());
         }
     });

  return retValue;
}

// Then when calling this method you can do something like:
CountryCodeObservable observable = onCountryPickerClick();

observable.addObserver((obj, arg) -> {
   // arg is the value that changed. You'll probably need to cast it to
   // a string
});

上面的示例允许您添加多个 observable。对于您的用例而言,这可能太多了,我只是认为它说明了另一种方法以及这种情况的异步性。

同样,还有更多的方法可以解决这个问题,关键是你不能简单地返回一个字符串并希望它在用户选择任何东西时改变。

于 2020-09-14T08:32:11.270 回答
1

String abc = onCountryPickerClick();被调用时,该selected_country_code值将被分配给abc

当您CountryCodePicker.OnCountryChangeListener的 'onCountrySelected()方法被调用时,ccp.getSelectedCountryCodeWithPlus();' 值被分配给selected_country_code. 由于String是不可变的,因此更改selected_country_code' 的值不会更改 的值abc,也return selected_country_code;不会调用 。

一种可能的解决方案是更改您的CountryCodePicker.OnCountryChangeListener匿名实现以将所选国家/地区值分配给abc例如

@Override
public void onCountrySelected() {
       selected_country_code = ccp.getSelectedCountryCodeWithPlus();
       abc = selected_country_code
}
于 2020-09-14T08:34:03.220 回答