gpt4 book ai didi

android - 如何在微调器中添加默认文本?

转载 作者:行者123 更新时间:2023-11-29 17:45:17 26 4
gpt4 key购买 nike

我是 android 的新手,我知道微调器的访问权限,但我想在微调器中添加默认文本并希望设置如下图所示的布局,任何人都可以帮助我吗,

enter image description here

    final String[] items = new String[] {"One", "Two", "Three"};
final ArrayAdapter<String> adapter123 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, items);

sp3.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View w) {
new AlertDialog.Builder(RegistrationForm.this)
.setTitle("the prompt")
.setAdapter(adapter123, new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {

dialog.dismiss();
}
}).create().show();
}
});

最佳答案

我对你的情况的建议是使用 Button最初设置文本并设置 gravity (不是 layout_gravity)到 left|center_vertical而不是打开 AlertDialog打开 PopupWindow 将该按钮设置为该 PopUpWindow 的 anchor 。在那PopUpWindow放置一个ListViewOnItemClick使用 setText(java.lang.CharSequence) 在该按钮中更改具有选定值 的文本

代码 fragment

该按钮的

XML

<Button
android:id="@+id/propertyBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/dp_4"
android:background="@drawable/property_btn_large"
android:ellipsize="marquee"
android:gravity="left|center_vertical"
android:marqueeRepeatLimit="marquee_forever"
android:minHeight="0dp"
android:minWidth="0dp"
android:onClick="showPropertyPopUp"
android:paddingLeft="42dp"
android:paddingRight="22dp"
android:shadowColor="@android:color/white"
android:shadowDx="1"
android:shadowDy="1"
android:shadowRadius="1"
android:text="@string/select_property" />

打开Java代码PopUpWindow在该按钮中单击

//don't forget to initialize that button in onCreate(...)
public void showPropertyPopUp(View v) {
propertyList = dbHelper.getAllProperties();
dbHelper.closeDB();

if(propertyList != null && propertyList.size() > 0) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popUpView = inflater.inflate(R.layout.pop_up, null, false);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.select_dropdown);
popupWindowProperty = new PopupWindow(popUpView, propertyBtn.getWidth(),
300, true);
popupWindowProperty.setContentView(popUpView);
popupWindowProperty.setBackgroundDrawable(new BitmapDrawable(getResources(),
bitmap));
popupWindowProperty.setOutsideTouchable(true);
popupWindowProperty.setFocusable(true);
popupWindowProperty.showAsDropDown(propertyBtn, 0, 0);
ListView dropdownListView = (ListView) popUpView.
findViewById(R.id.dropdownListView);
PropertyDropdownAdapter adapter = new PropertyDropdownAdapter(
AddIncomeActivity.this,
R.layout.row_pop_up_list, propertyList);
dropdownListView.setAdapter(adapter);
dropdownListView.setOnItemClickListener(this);
}
}

Button 上设置文本的代码在OnItemClick

PropertyInfo addPropertyInfo = propertyList.get(position);
String propertyName = addPropertyInfo.getPropertyName();
propertyBtn.setText(propertyName);
popupWindowProperty.dismiss();

pop_up布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
android:id="@+id/dropdownListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="@null"
android:fadeScrollbars="false" >
</ListView>

</LinearLayout>

点击该按钮的屏幕截图

enter image description here

ListView item点击后的截图 enter image description here

关于android - 如何在微调器中添加默认文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27199251/

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