如何在autoCompleteTextView中使用元素?

时间:2014-06-18 08:27:21

标签: android text autocomplete integer

我想将所选元素转换为Integer。当它完成后,我想在1-20之间添加一个随机数来选择整数。然后在Toast中显示该数字。

2 个答案:

答案 0 :(得分:0)

要将来自TextView的值转换为整数,只需使用以下代码:

EditText tViewNum = (EditText) rootView.findViewById(R.id.number);
String strWord = tViewNum.getText().toString();

Random r = new Random();
int i1 = r.nextInt(21 - 1) + 20;

String Randomiser = strWord + " " + il; //the +" "+ is used to add a space between the word and the random number. 

Toast.makeText(MainActivity.this, Randomiser + "//any other text you wish to include", Toast.LENGTH_SHORT).show();

请注意,此处给出的随机数介于1(含)和20(含)之间。

答案 1 :(得分:0)

使用widget forAutoCompleteTextView。

" CustomAutoCompleteTextView"

public class CustomAutoCompleteTextView extends AutoCompleteTextView {

  public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

@Override
protected void performFiltering(final CharSequence text, final int keyCode) {
    // String filterText = "";
    super.performFiltering(text, keyCode);


}

/**
 * After a selection, capture the new value and append to the existing
 * text
 */
@Override
protected void replaceText(final CharSequence text) {
    super.replaceText(text);


 }
}

您的Xml将如下所示:[取决于您添加小部件的位置]

    <com.example.widget.CustomAutoCompleteTextView
        android:id="@+id/sent_message_to"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_margin="10dip"
        android:layout_weight="1"

        android:imeOptions="actionSend"

        android:hint="name"
        android:gravity="left|center"
        android:padding="10dip"
        android:textColor="@color/green"
        android:textSize="18dp"
        android:visibility="visible"
        android:selectAllOnFocus="true"
        android:inputType="textPersonName"
        android:completionThreshold="3"

        />

主要课程

您可以为列表值设置适配器或声明数组

private AutoCompleteAdapter mAutoCompleteAdapter;
private ArrayList<String> mArray = new ArrayList<String>();
private CustomAutoCompleteTextView mAutoFillTextView;
.....

mAutoFillTextView = mViewUtils.createAutoFillTextView(view.findViewById(R.id.sent_message_to), false);

        mAutoCompleteAdapter = new AutoCompleteAdapter(getActivity(), mArray);
        mAutoFillTextView.setAdapter(mAutoCompleteAdapter);

和       mAutoFillTextView.addTextChangedListener(new TextWatcher(){             @覆盖             public void onTextChanged(final CharSequence s,int start,int before,int count){

            try {


                mArray.clear();
                String string = s.toString().trim();

                if (mAutoFillTextView.getThreshold() <= string.length() && mAllowRequest) {
                    //GET DATA  TO LIST

                } 


            } catch (NullPointerException ignored) {
            }

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });