gpt4 book ai didi

android - 如何在android fragment中添加带有 ListView 和搜索功能的自定义对话框

转载 作者:行者123 更新时间:2023-11-29 19:08:54 25 4
gpt4 key购买 nike

我想创建如下所示的完全相同的 View ,

enter image description here

我使用了showDialog 函数来显示对话框。

private void showDialog() {
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment prev = getFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);

// Create and show the dialog.
DialogFragment newFragment = PopDialogFragment.newInstance();
newFragment.show(ft, "dialog");
}

帮我实现显示listview和搜索listview的功能。

编辑代码,

public class BookingFragment extends Fragment {

CardView cardFrom, cardTo;
TextView titleFromCity, titleToCity;
String[] nameOfCities;
private Context context;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_bookings, container, false);
titleFromCity = view.findViewById(R.id.textFromCity);
titleToCity = view.findViewById(R.id.textToCity);
cardFrom = view.findViewById(R.id.cardViewFrom);
cardTo = view.findViewById(R.id.cardViewTo);
cardFrom.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Choose City");

nameOfCities = getResources().getStringArray(R.array.cities);

builder.setItems(nameOfCities, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
titleFromCity.setText(nameOfCities[which]);

});

AlertDialog dialog = builder.create();
dialog.show();
}
});


cardTo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
test();
}
});

return view;
}

private void showDialog() {
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment prev = getFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);

DialogFragment newFragment = PropDialogFragment.newInstance();
newFragment.show(ft, "dialog");
}

private void test() {
final Dialog dialog_data = new Dialog(context);

dialog_data.requestWindowFeature(Window.FEATURE_NO_TITLE);

dialog_data.getWindow().setGravity(Gravity.CENTER);

dialog_data.setContentView(R.layout.alert_label_editor);

WindowManager.LayoutParams lp_number_picker = new WindowManager.LayoutParams();
Window window = dialog_data.getWindow();
lp_number_picker.copyFrom(window.getAttributes());

lp_number_picker.width = WindowManager.LayoutParams.MATCH_PARENT;
lp_number_picker.height = WindowManager.LayoutParams.WRAP_CONTENT;

window.setGravity(Gravity.CENTER);
window.setAttributes(lp_number_picker);

TextView alertdialog_textview = (TextView) dialog_data.findViewById(R.id.alertdialog_textview);
alertdialog_textview.setText("Search City");
alertdialog_textview.setHint("Search");

Button dialog_cancel_btn = (Button) dialog_data.findViewById(R.id.dialog_cancel_btn);
dialog_cancel_btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if(dialog_data != null)
{
dialog_data.dismiss();
dialog_data.cancel();
}

}
});

EditText filterText = (EditText) dialog_data.findViewById(R.id.alertdialog_edittext);
ListView alertdialog_Listview = (ListView) dialog_data.findViewById(R.id.alertdialog_Listview);
alertdialog_Listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
final ArrayAdapter<String> adapter = new ArrayAdapter<>(context, android.R.layout.simple_list_item_single_choice, nameOfCities);
alertdialog_Listview.setAdapter(adapter);
alertdialog_Listview.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id)
{


if(dialog_data != null)
{
dialog_data.dismiss();
dialog_data.cancel();
}
}
});

filterText.addTextChangedListener(new TextWatcher()
{
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{

}

@Override
public void afterTextChanged(Editable s)
{
adapter.getFilter().filter(s);
}
});

dialog_data.show();
}

最佳答案

enter image description here

    final Dialog dialog_data = new Dialog(context);

dialog_data.requestWindowFeature(Window.FEATURE_NO_TITLE);

dialog_data.getWindow().setGravity(Gravity.CENTER);

dialog_data.setContentView(R.layout.custom_alertdialog);

WindowManager.LayoutParams lp_number_picker = new WindowManager.LayoutParams();
Window window = dialog_data.getWindow();
lp_number_picker.copyFrom(window.getAttributes());

lp_number_picker.width = WindowManager.LayoutParams.MATCH_PARENT;
lp_number_picker.height = WindowManager.LayoutParams.WRAP_CONTENT;

window.setGravity(Gravity.CENTER);
window.setAttributes(lp_number_picker);

TextView alertdialog_textview = (TextView) dialog_data.findViewById(R.id.alertdialog_textview);
alertdialog_textview.setText(selected_string);
alertdialog_textview.setHint(selected_string);

Button dialog_cancel_btn = (Button) dialog_data.findViewById(R.id.dialog_cancel_btn);
dialog_cancel_btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if(dialog_data != null)
{
dialog_data.dismiss();
dialog_data.cancel();
}

}
});

EditText filterText = (EditText) dialog_data.findViewById(R.id.alertdialog_edittext);
ListView alertdialog_Listview = (ListView) dialog_data.findViewById(R.id.alertdialog_Listview);
alertdialog_Listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
final ArrayAdapter<String> adapter = new ArrayAdapter<>(context, android.R.layout.simple_list_item_single_choice, array_data);
alertdialog_Listview.setAdapter(adapter);
alertdialog_Listview.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id)
{
Utility.hideKeyboard(context,v);


//data.setText(String.valueOf(a.getItemAtPosition(position)));


if(dialog_data != null)
{
dialog_data.dismiss();
dialog_data.cancel();
}
}
});

filterText.addTextChangedListener(new TextWatcher()
{
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{

}

@Override
public void afterTextChanged(Editable s)
{
adapter.getFilter().filter(s);
}
});

dialog_data.show();

custom_alertdialog.xml

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/alertdialog_linearlayout"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="@null">

<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_marginTop="20dp"
android:background="#FFFFFF">

<TextView
android:id="@+id/alertdialog_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="search"
android:textColor="#000000"
android:textSize="16sp"
android:layout_marginTop="220dp"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:layout_marginRight="20dp"
android:layout_marginEnd="20dp"
android:gravity="center"
android:drawableLeft="@drawable/ic_launcher"
android:drawableStart="@drawable/ic_launcher"
android:drawablePadding="10dp"/>

<EditText
android:id="@+id/alertdialog_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:textStyle="normal"
android:textColorHint="#939598"
android:textColor="#4b4f54"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:layout_marginRight="20dp"
android:layout_marginEnd="20dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:maxLines="1"
android:textCursorDrawable="@null"
android:gravity="start"
android:inputType="text"
android:theme="@style/alertdialog_edittextTheme"
android:layout_gravity="start" />

<ListView
android:id="@+id/alertdialog_Listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:layout_marginRight="20dp"
android:layout_marginEnd="20dp"
android:choiceMode="singleChoice"/>

</LinearLayout>

<Button
android:id="@+id/dialog_cancel_btn"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:text="X"
android:textColor="#000000"
android:gravity="center"
android:layout_alignParentTop="true"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:layout_marginTop="5dp"
android:background="@drawable/round_button"/>

</RelativeLayout>

round_button.xml


<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="oval"
android:thicknessRatio="1.9"
android:useLevel="false" >

<solid android:color="@android:color/white" />

<size android:width="100dp"
android:height="100dp"/>

<stroke android:width="1dp"
android:color="#000000"/>

关于android - 如何在android fragment中添加带有 ListView 和搜索功能的自定义对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46103090/

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