2016-11-27 68 views
0

我試圖實現以下代碼片段。當然,我做了所有必要的更改以避免語法和其他錯誤。片段上沒有建議的AutoCompleteTextView

public class MainActivity extends ActionBarActivity { 

private AutoCompleteTextView autoCompleteTextView; 

@Override 

protected void onCreate(Bundle savedInstanceState) { 

super.onCreate(savedInstanceState); 

setContentView(R.layout.activity_main); 

autoCompleteTextView = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView); 

String[]dataStorage = {"Nigeria", "Ghana", "Mali", "South Africa", "Cameroon", "Niger", "Algeria", "Zimbabwe"}; 

ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), android.R.layout.select_dialog_item, dataStorage); 

autoCompleteTextView.setThreshold(2); 

autoCompleteTextView.setAdapter(adapter); 

} 

@Override 

public boolean onCreateOptionsMenu(Menu menu) { 

// Inflate the menu; this adds items to the action bar if it is present. 

getMenuInflater().inflate(R.menu.menu_main, menu); 

return true; 

} 

@Override 

public boolean onOptionsItemSelected(MenuItem item) { 

// Handle action bar item clicks here. The action bar will 

// automatically handle clicks on the Home/Up button, so long 

// as you specify a parent activity in AndroidManifest.xml. 

int id = item.getItemId(); 

//noinspection SimplifiableIfStatement 

if (id == R.id.action_settings) { 

return true; 

} 

return super.onOptionsItemSelected(item); 

} 
} 

它運行時沒有任何錯誤,但我在第一個字符後沒有得到搜索建議。首先在全屏模式下用稱爲提交的按鈕打開屏幕鍵盤。我無法得到任何建議,也許我不能看到它,因爲這個全屏鍵盤。

回答

1

如果問題僅僅是全屏鍵盤隱藏在搜索建議,您可以通過設置flagNoExtractUi選項禁用全屏鍵盤:

<AutoCompleteTextView 
    android:id="@+id/autoCompleteTextView" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:imeOptions="flagNoExtractUi" /> 

除此之外,你的代碼是好的,而AutoCompleteTextView應該工作正確。

+0

這行到XML,但沒有任何改變。 – appkovacs

+0

當您鍵入某些字符以及整個activity_main.xml時,您可以發佈您的應用的屏幕截圖嗎? – weibeld

+0

它在'TableRow'中,如下所示。應用程序屏幕看起來很簡單 ' ' – appkovacs

1

從文檔:

setThreshold(int threshold) 

指定的最小字符數目用戶具有被示出的下拉列表之前鍵入編輯框。

您正在將閾值設置爲2個字符,然後進行預測。此外,提供給適配器測試數據的列表是非常獨特的

嘗試的閾值設置爲0,然後再試一次

還應該指定數組適配器的類型:我添加

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.select_dialog_item, dataStorage); 
+0

我將它設置爲0,但也許它應該爲「倪」。 – appkovacs

+0

我已經編輯了答案,你也應該指定arrayAdapter ArrayAdapter的類型 Pomanh

+0

我修改了代碼,但沒有改變。 – appkovacs