我是新的 Android 学习者,这是我的问题。
我正在使用 android API 15+。我的目标是AutoCompleteTextView
在ActionBar
. Custom
因为我会有一个按钮,AutoCompleteTextView
允许用户使用他们的声音而不是输入一些单词。
我怎样才能做到这一点?我应该使用一些适配器吗?创建自定义小部件?我没有找到办法做到这一点。
我是新的 Android 学习者,这是我的问题。
我正在使用 android API 15+。我的目标是AutoCompleteTextView
在ActionBar
. Custom
因为我会有一个按钮,AutoCompleteTextView
允许用户使用他们的声音而不是输入一些单词。
我怎样才能做到这一点?我应该使用一些适配器吗?创建自定义小部件?我没有找到办法做到这一点。
我刚刚能够完成这个。它真的很简单。您需要 AsyncTask,因为 Android 4+ 不允许 UI 线程上的网络内容。
步骤1
所以我在我的 Activity 类中创建了一个私有 AsyncTask 类。
public class AutoCompleteHints extends AsyncTask<String, Void, StringBuilder> {
private boolean whichPoint;
private HttpURLConnection conn = null;
private StringBuilder jsonResults = new StringBuilder();
public AutoCompleteHints(boolean whichPoint) {
this.whichPoint = whichPoint;
}
@Override
protected StringBuilder doInBackground(String... str) {
try {
StringBuilder sb = new StringBuilder(
"http://maps.googleapis.com/maps/api/geocode/json?" + "address="
+ URLEncoder.encode(str[0], "utf-8") + "&sensor=true");
URL url = new URL(sb.toString());
Log.d("Taxeeta", url.toString() + " Called");
conn = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(conn.getInputStream());
// Load the results into a StringBuilder
int read;
char[] buff = new char[1024];
while ((read = in.read(buff)) != -1) {
jsonResults.append(buff, 0, read);
}
} catch (MalformedURLException e) {
Log.e("Taxeeta", "Error processing Places API URL", e);
} catch (IOException e) {
Log.e("Taxeeta", "Error connecting to Places API", e);
} finally {
if (conn != null) {
conn.disconnect();
}
}
return jsonResults;
}
@Override
protected void onPostExecute(final StringBuilder result) {
Double latitude, longitude;
try {
// Create a JSON object hierarchy from the results
JSONObject jsonObj = new JSONObject(result.toString());
JSONObject location = ((JSONObject) jsonObj.getJSONArray("results").get(0))
.getJSONObject("geometry").getJSONObject("location");
latitude = location.optDouble("lat");
longitude = location.optDouble("lng");
Log.d("Taxeeta", "Received Latitude " + latitude + ": Longitude" + longitude);
if (whichPoint == PICKUP) {
source = new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6));
startLocation.placeMarker(source);
Log.d("Taxeeta", "Source Done");
} else if (whichPoint == DROP) {
destination = new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6));
endLocation.placeMarker(destination);
Log.d("Taxeeta", "Destination Done");
} else
Log.e("Taxeeta", "Something messed up here.");
// TODO : Path from source to destination
adjustZoomAndSpan();
} catch (JSONException e) {
Log.e("Taxeeta", "Cannot process JSON results", e);
}
}
}
第2步
调用 AsyncTask onItemClick
在 onCreate
fromAutoComplete = new AutoComplete(this, R.layout.fromautocomplete);
fromAutoComplete.setNotifyOnChange(true);
fromAddress = (AutoCompleteTextView) findViewById(R.id.fromAddress);
fromAddress.setAdapter(fromAutoComplete);
fromAddress.setOnItemClickListener(this);
对于 onItem 点击 autocompletetextView
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.e("Taxeeta", parent.getId() + ":" + view.getId() + ":" + id);
ArrayList<String> resultList = null;
String str = (String) parent.getItemAtPosition(position);
if (view.getId() == R.id.fromautocomplete)
new AutoCompleteHints(PICKUP).execute(str) ;
else if (view.getId() == R.id.toautocomplete)
new AutoCompleteHints(DROP).execute(str) ;
}