如何将字符串分解为微调器和edittext值?

时间:2016-03-14 10:00:29

标签: android spinner

我创建了一个微调器,它定义了STD(区域)代码和一个定义电话号码的编辑文本。

 <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="@dimen/fup_ll_height"
                android:orientation="horizontal"
                android:layout_marginTop="@dimen/fup_ll_margintop">
                <Spinner
                    android:id="@+id/phone1_spinner"
                    android:layout_width="@dimen/fup_spinner_width"
                    android:layout_height="@dimen/fup_spinner_height"
                    android:background="@drawable/spinner_phone"
                    android:gravity="center"
                    android:spinnerMode="dropdown"
                    android:layout_marginTop="@dimen/fup_spinner_margintop"
                    android:layout_marginRight="@dimen/fup_spinner_marginright"
                    />

                <EditText
                    android:id="@+id/Phone1EditText"
                    android:layout_width="fill_parent"
                    android:layout_height="@dimen/fup_edittext_phone1_height"
                    android:background="@drawable/input_box"
                    android:gravity="center_vertical"
                    android:hint="Phone1"
                    android:inputType="phone"
                    android:layout_marginTop="@dimen/fup_edittext_margintop"
                    android:paddingLeft="@dimen/fup_edittext_phone2_paddingleft"
                    android:singleLine="true"
                    android:textSize="@dimen/fup_commontextview_textsize" />
            </LinearLayout>

包括来自服务器的区域代码的电话号码是9或10位数。编辑文本(电话号码)必须包含7(7)个号码。

在编程中我如何检查areacode是两位数还是三位数?

我的编码是: -

private String[] areaCodes;
    private String phone1;
private ArrayAdapter<String> adapter;
private Spinner phone1Spinner

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
phone1Spinner = (Spinner) view.findViewById(R.id.phone1_spinner);
 Phone1EditText = (EditText) view.findViewById(R.id.Phone1EditText);

areaCodes = new String[]{"02", "03", "04", "08", "09", "050", "052", "054", "072", "073", "074", "077"};
        adapter = new ArrayAdapter<String>(context, R.layout.list_spinner_phone, areaCodes);        
        adapter.setDropDownViewResource(R.layout.list_spinner_phone);        
        phone1Spinner.setAdapter(adapter);
 phone1Spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                phone1 = parent.getItemAtPosition(position).toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
//when i send data to server..its ok for me
phone1 = phone1 + Phone1EditText.getText().toString();
//but when retrieve data from server what should i do as data is in combined form means(areacode+phone number), either in 9 or 10 digits????

}

我试过这个,但没有解决方案。

if (userProfileDataModel1.getUser().getPhone1().toString().length() == 9) {
                if (!(userProfileDataModel1.getUser().getPhone1().substring(1, 2).equals(null))) {
                    int spinnerPosition = adapter.getPosition(userProfileDataModel1.getUser().getPhone1().substring(1, 3));
                    phone1Spinner.setSelection(spinnerPosition);
                }

                Phone1EditText.setText(userProfileDataModel1.getUser().getPhone1().substring(2, 9));
            } else {
                if (!(userProfileDataModel1.getUser().getPhone1().substring(0, 3).equals(null))) {
                    int spinnerPosition = adapter.getPosition(userProfileDataModel1.getUser().getPhone1().substring(0, 3));
                    phone1Spinner.setSelection(spinnerPosition);
                }
                Phone1EditText.setText(userProfileDataModel1.getUser().getPhone1().substring(3, 10));
            }
        }

1 个答案:

答案 0 :(得分:1)

您应该尝试以下代码: -

String phone1;
YOUR_SPINNER.setOnItemSelectedListener(new OnItemSelectedListener() 
{    
 @Override
 public void onItemSelected(AdapterView adapter, View v, int i, long lng) {

   phone1 = parent.getItemAtPosition(i).toString();

     ///AND THAN AFTER FIND THE LENGTH OF THE SELECTED TEXT

  int length= phone1.length();



} 
  @Override     
  public void onNothingSelected(AdapterView<?> parentView) 
 {         

 }
});