gpt4 book ai didi

android - 微调器提示不显示

转载 作者:行者123 更新时间:2023-11-29 19:15:40 24 4
gpt4 key购买 nike

我想使用像“选择城市”这样的微调提示。我确实在布局 android:prompt="@string/select_city" 的字符串布局中为 select_city 设置了一个值,但它没有用。还尝试使用 sp_city.setPrompt("Select City"); 也没有用。

我的问题是什么?如何修复它并设置提示?

布局:

<Spinner
android:id="@+id/spinner_poi_city"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/btn_dropdown"
android:spinnerMode="dropdown"/>

类:

public class FirstPOIPreference extends DialogPreference {

private Spinner sp_city;
private ArrayAdapter<String> adapter;
private POI poiDialog = new POI();

public FirstPOIPreference(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

private void init() {
setPersistent(false);
setDialogLayoutResource(R.layout.pref_poi_dialog);
setPositiveButtonText(android.R.string.ok);
setNegativeButtonText(android.R.string.cancel);
}

@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
initViews(view);
}

private void initViews(View view) {
sp_city = (Spinner) view.findViewById(R.id.spinner_poi_city);
sp_city.setPrompt("City");
String[] arrayCity = new String[]{"Erie", "Pittsburgh", "Cleveland", "Buffalo", "Niagara Falls", "Jamestown"};
adapter = new ArrayAdapter <> (this.getContext(), android.R.layout.simple_spinner_dropdown_item, arrayCity);
sp_city.setAdapter(adapter);

sp_city.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView <?> parentView, View selectedItemView, int position, long id) {
poiDialog.setCity(sp_city.getSelectedItem().toString());
}

@Override
public void onNothingSelected(AdapterView <?> parentView) {
}
});
}

}

最佳答案

你可以这样做,我已经做到了一些棘手的方式。它可以帮助你

private void initViews(View view) {

sp_city = (Spinner) view.findViewById(R.id.spinner_poi_city);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item) {

@Override
public View getView(int position, View convertView, ViewGroup parent) {

View v = super.getView(position, convertView, parent);
if (position == getCount()) {
((TextView) v.findViewById(android.R.id.text1)).setText("");
((TextView) v.findViewById(android.R.id.text1)).setHint(getItem(getCount())); //"Hint to be displayed"
}
return v;
}

@Override
public int getCount() {
return super.getCount() - 1; // you dont display last item. It is used as hint.
}
};
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter.add("Erie");
adapter.add("Pittsburgh");
adapter.add("Cleveland");
adapter.add("Buffalo");
adapter.add("Niagara Falls");
adapter.add("Jamestown");
adapter.add("Select City"); //This is the text that will be displayed as hint.

sp_city.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
String spinnerValue = String.valueOf(sp_city.getSelectedItem().toString());
if (!spinnerValue.equalsIgnoreCase("Select City")) {
//do your code
Toast.makeText(this, sp_city.getSelectedItem().toString(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});

sp_city.setAdapter(adapter);
sp_city.setSelection(adapter.getCount()); //set the hint the default selection so it appears on launch.
}

关于android - 微调器提示不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43601160/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com