Android 如何在autoCompleteTextView中使用元素?

Android 如何在autoCompleteTextView中使用元素?,android,text,autocomplete,integer,Android,Text,Autocomplete,Integer,我想将choosen元素转换为整数。完成后,我想在1-20之间添加一个随机数,然后选择整数。然后在Toast中显示该数字。要将文本视图中的值转换为整数,只需使用以下代码: EditText tViewNum = (EditText) rootView.findViewById(R.id.number); String strWord = tViewNum.getText().toString(); Random r = new Random(); int i1 = r.nextInt(21 -

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

要将文本视图中的值转换为整数,只需使用以下代码:

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之间。

使用小部件获取AutoCompleteTextView

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.addTextChangedListenernew TextWatcher{ @凌驾 public void onTextChangedfinal字符序列,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) {
        }
    });

将元素转换为整数?您试图转换的元素是什么?AutoCompleteTextView的元素。您面临什么问题?我不知道怎么做。您知道什么是AutoCompleteTextView吗?为什么不使用edittext?是的,我认为这将是解决方案,但程序如何知道键入edittext的内容。这与f不同或者,如果用户键入pear或apple.com,因为您使用此行从TextView获取数据:int num=Integer.parseIntViewnum.getText.toString;@mlaferla Toast将字符串作为第二个参数,而不是int。请编辑它,如果第一行中存在类不匹配,则必须是EditText。请将上下文更改为this或getApplica我不知道你为什么要使用rootView。你介意解释一下吗?@Adrian出于你的目的,你应该使用EditText而不是AutoCompleteTextView。上面的代码使用的是EditText。如果你想使用ACTV,你需要先将值存储在数组中。@mlaferla你为什么要使用rootView?