我想在android中创建一个控件,用户可以通过键盘输入或通过下拉列表(微调器)输入。
实际上我在spinner中的数组中硬编码的值并不详尽,因此用户也应该可以选择通过虚拟键盘输入。
那么用户可以通过键盘输入还是从列表中选择?
我如何在Android中实现这一目标?
答案 0 :(得分:0)
您可以构建自己的视图:
public class ServerPreference extends DialogPreference {
private String value;
private EditText editText;
private Spinner spinner;
private final int server_list_id;
ArrayList<String> servers;
public ServerPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setPersistent(false);
setDialogLayoutResource(R.layout.server_preference_layout);
servers = new ArrayList<String>();
Resources r = context.getResources();
if (MySettings.KEY_ASR_SERVER_HOST.equals(getKey())) {
server_list_id = R.array.asrServers;
} else if (MySettings.KEY_LOG_SERVER_HOST.equals(getKey())) {
server_list_id = R.array.logServers;
} else {
server_list_id = R.array.servicesServers;
}
Collections.addAll(servers, r.getStringArray(server_list_id));
}
@Override
protected void onBindDialogView(View view) {
editText = (EditText)view.findViewById(R.id.server_preference_text);
spinner = (Spinner)view.findViewById(R.id.server_preference_spinner);
SharedPreferences pref = getSharedPreferences();
value = pref.getString(getKey(), "");
editText.setText(value);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
view.getContext(), server_list_id, android.R.layout.simple_spinner_item );
adapter.setDropDownViewResource( android.R.layout.simple_dropdown_item_1line );
spinner.setAdapter( adapter );
updateSpinner();
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if ( position != 0 ) {
value = servers.get(position);
editText.setText(value);
}
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
editText.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
updateSpinner();
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
}
});
super.onBindDialogView(view);
}
void updateSpinner() {
value = editText.getText().toString();
int index = servers.indexOf(value);
if ( index == -1 )
index = 0;
spinner.setSelection(index);
}
@Override
protected void onDialogClosed(boolean positiveResult) {
if ( positiveResult ) {
SharedPreferences.Editor editor = getEditor();
editor.putString(getKey(),value);
editor.commit();
}
super.onDialogClosed(positiveResult);
}
public String getValue() {
return value;
}
void setValue(String value) {
this.value = value;
}
}
自定义视图的布局:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/server_preference_text" android:inputType="text">
<requestFocus></requestFocus>
</EditText>
<Spinner android:layout_height="wrap_content" android:id="@+id/server_preference_spinner" android:layout_width="match_parent"></Spinner>
</LinearLayout>
如果要使用此视图,只需在XML文件中定义:
<xxx.xxxxxx.xxxxx.settings.debug.ServerPreference
android:title="ASR/VVS Server"
android:persistent="true"
android:positiveButtonText="OK"
android:dialogTitle="ASR/VVS Server"
android:key="SERVER_NAME"
android:negativeButtonText="Cancel"
android:entryValues="@array/asrServers"
android:entries="@array/asrServers"/>