2014-06-18 39 views

回答

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

是的,我認爲這將是解決方案,但程序如何知道輸入到EditText中。例如,如果用戶輸入pear或apple,則不一樣。 – Adrian

+0

,因爲你使用這條線從TextView獲取數據:'int num = Integer.parseInt(tViewNum.getText()。toString());' –

+0

@mlaferla Toast將String作爲第二個參數,而不是int。請編輯它,並且第一行中存在類不匹配,它必須是EditText。請將上下文更改爲this或getApplicationContext()。我不知道你爲什麼使用'rootView'。你介意解釋嗎? – Aniruddha

0

使用自動完成文本視圖控件。

「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(新TextWatcher(){ @覆蓋 公共無效之前onTextChanged(最終CharSequence中,詮釋開始,詮釋,詮釋計數){

  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) { 
     } 
    }); 
相關問題