【发布时间】:2021-03-17 00:47:39
【问题描述】:
我有一个由多个片段或活动读取的警报对话框,在一个实例中,警报对话框将显示一个带有 2 个 editText 选项以及标准正负按钮按下的 listView,我想将 listView 定位在 editTexts 之后我被困住了。
一切都正常显示,但定位是我卡住的地方。
对话框类
public class addTaskDialog extends DialogFragment {
String mString;
taskDialogListener listener;
ArrayList<String> tradeTitles;
/*
* Class interface from jobRoomDetails, interfaces are called from the parent activity class
*/
public interface taskDialogListener {
void onDialogPositiveClick(DialogFragment dialog);
void onDialogNegativeClick(DialogFragment dialog);
void onDialogListSelect();
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
try{
listener = (taskDialogListener) context;
} catch(ClassCastException e) {
throw new ClassCastException(getActivity().toString() + "must implement taskDialogListener");
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
AlertDialog alert = builder.create();
mString = getArguments().getString("title");
tradeTitles = getArguments().getStringArrayList("tradeTitleArray");
builder.setTitle(mString);
//Inflate the layout dialog_add_task.xml
LayoutInflater inflater = requireActivity().getLayoutInflater();
//Pattern matcher for literal strings from different addTaskDialog calls
Pattern p = Pattern.compile("Add new task +");
Matcher m = p.matcher(mString);
Pattern q = Pattern.compile("Add Task +");
Matcher n = q.matcher(mString);
/*
*Test for new task and add trade names, this means that the calling window is jobRoomDetails and this is a new task
* and it will be assigned to a trade upon creation
*/
if(m.find()){
Log.d("Line: 56","Test for trade name" + tradeTitles.toString());
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.select_dialog_singlechoice, tradeTitles);
//ListView list = (ListView) alert.findViewById(R.id.tradeItems);
builder.setView(inflater.inflate(R.layout.dialog_add_task, null))
.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Save the new task to task hashmap
listener.onDialogPositiveClick(addTaskDialog.this);
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Cancel operation and return
listener.onDialogNegativeClick(addTaskDialog.this);
}
})
.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
}
});
} else if (n.find()){
builder.setView(inflater.inflate(R.layout.dialog_add_task, null))
.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Save the new task to task hashmap
listener.onDialogPositiveClick(addTaskDialog.this);
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Cancel operation and return
listener.onDialogNegativeClick(addTaskDialog.this);
}
});
} else {
builder.setView(inflater.inflate(R.layout.dialog_add_task, null))
.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Save the new task to task hashmap
listener.onDialogPositiveClick(addTaskDialog.this);
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Cancel operation and return
listener.onDialogNegativeClick(addTaskDialog.this);
}
});
}
return builder.create();
}
}
dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<TextView
style="@style/textBox"
android:layout_margin="20dp"
android:layout_marginTop="5dp"
android:text="@string/add_task_ex"/>
<EditText
android:id="@+id/taskNameEditText"
style="@style/editTextBox"
android:hint="@string/add_task_name"
android:inputType="text"/>
<EditText
android:id="@+id/taskCost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="0"
android:textSize="25sp"
android:inputType="numberDecimal"/>
<FrameLayout
android:id="@+id/tradeListItems"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="@+id/tradeItems"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</FrameLayout>
</LinearLayout>
</androidx.appcompat.widget.LinearLayoutCompat>
有一次我尝试通过 listView 添加它但没有成功,我认为 onCreateDialog 上的返回类型可能会阻碍事情,但我不确定,我认为返回类型是必要的,因为我带来通过争论,但我可能是错的。一如既往地感谢您的帮助。
【问题讨论】:
标签: java android xml android-fragments