我需要使用类似setMultiChoiceItems样式的对话框,但需要更多信息,所以我使用的是自定义布局。
我认为我的代码非常庞大且混乱。我想知道是否还有其他更简单的方法来做到这一点。
####正在运行的对话框:####
####活动布局:R.layout.activity_formsmultipleselectcustom_java ####
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FormsMultipleSelectCustomJava">
<Button
android:text="Dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/btndialog" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" android:layout_marginRight="8dp"
app:layout_constraintStart_toStartOf="parent" android:layout_marginLeft="8dp"
android:layout_marginStart="8dp" app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="8dp"
app:layout_constraintBottom_toTopOf="@+id/btnenviar"/>
<Button
android:text="Send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnenviar"
app:layout_constraintTop_toBottomOf="@+id/btndialog" android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp" android:layout_marginRight="8dp"
app:layout_constraintStart_toStartOf="parent" android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
####布局:R.layout.activity_formsmultipleselectcustom_java_lines ####
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:gravity="center">
<ImageView
android:id="@+id/imgvicon"
tools:srcCompat="@tools:sample/avatars"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
android:adjustViewBounds="true"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_weight="1">
<TextView
android:text="Titulo"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/txtv_titulo"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp" android:textSize="18sp"/>
<TextView
android:text="Descrição"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/txtv_desc"
android:layout_marginLeft="8dp" android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp" android:visibility="visible" android:layout_marginTop="3dp"/>
</LinearLayout>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/chkbox"
android:layout_marginEnd="8dp" android:layout_marginRight="8dp"
android:layout_marginTop="8dp" android:layout_marginBottom="8dp"
/>
</LinearLayout>
#### ALItensValuesList ####
public class ALItensValuesList {
private int icon;
private String value;
private String titulo;
private String descricao;
private Boolean isChecked;
public ALItensValuesList (int icon, String value, String titulo, String descricao) {
this.icon = icon;
this.value = value;
this.titulo = titulo;
this.descricao = descricao;
this.isChecked = false;
}
int getIcon() { return icon; }
public String getValue() { return value; }
public String getTitulo() { return titulo; }
String getDescricao() { return descricao; }
public Boolean getChecked() { return isChecked; }
public void setChecked(Boolean isChecked) {
this.isChecked = isChecked;
}
}
#### ALItensInterface(适配器中ItemChecked的接口)####
public interface ALItensInterface {
void onItemChecked(int position);
}
#### ALItensValuesAdapter ####
public class ALItensValuesAdapter extends BaseAdapter {
private ArrayList<ALItensValuesList> mData;
private Context mContext;
private ALItensInterface minterface;
public ALItensValuesAdapter(ArrayList<ALItensValuesList> aldata, Context context, ALItensInterface minterface) {
this.mData = aldata;
this.mContext = context;
this.minterface = minterface;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = Objects.requireNonNull(mInflater).inflate(R.layout.activity_formsmultipleselectcustom_java_lines, null);
}
ImageView icon = convertView.findViewById(R.id.imgvicon);
icon.setImageResource(mData.get(position).getIcon());
TextView titulo = convertView.findViewById(R.id.txtv_titulo);
titulo.setText(mData.get(position).getTitulo());
TextView decricao = convertView.findViewById(R.id.txtv_desc);
decricao.setText(mData.get(position).getDescricao());
CheckBox checkbox = convertView.findViewById(R.id.chkbox);
checkbox.setChecked(mData.get(position).getChecked());
// When you click on the item line.
convertView.setOnClickListener(v -> {
checkbox.toggle();
this.minterface.onItemChecked(position);
});
// When you click directly on the checkbox
checkbox.setOnClickListener(v -> {
this.minterface.onItemChecked(position);
});
return convertView;
}
}
####对话框的活动####
public class FormsMultipleSelectCustomJava extends AppCompatActivity {
String selectItensServer;
String[] selectInitItens;
ArrayList<String> selectedItemsList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_formsmultipleselectcustom_java);
selectItensServer = "0b2,0e5,0f6,0h8"; // Example of random value received by the server.
selectInitItens = selectItensServer.split(",");
// Fixed values that fill the dialog
ArrayList<ALItensValuesList> AlertDialogitemList = new ArrayList<>();
AlertDialogitemList.add(new ALItensValuesList(R.drawable.atrib_bicycle, "0a1", "Test 1", "Lorem ipsum dolor sit amet"));
AlertDialogitemList.add(new ALItensValuesList(R.drawable.atrib_btc, "0b2", "Test 2", "Duis at ullamcorper quam"));
AlertDialogitemList.add(new ALItensValuesList(R.drawable.atrib_car, "0c3", "Test 3", "Aenean aliquam sit amet nibh eget bibendum"));
AlertDialogitemList.add(new ALItensValuesList(R.drawable.atrib_credit_card, "0d4", "Test 4", "Sed a est vel velit rutrum porta"));
AlertDialogitemList.add(new ALItensValuesList(R.drawable.atrib_gamepad, "0e5", "Test 5", "Phasellus pellentesque arcu et fermentum tempus"));
AlertDialogitemList.add(new ALItensValuesList(R.drawable.atrib_gift, "0f6", "Test 6", "Integer arcu leo, consequat eget tempus sed"));
AlertDialogitemList.add(new ALItensValuesList(R.drawable.atrib_paypal, "0g7", "Test 7", "Pellentesque habitant morbi tristique senectus"));
AlertDialogitemList.add(new ALItensValuesList(R.drawable.atrib_taxi, "0h8", "Test 8", "Proin iaculis, velit ac consectetur bibendum, orci nisi scelerisque"));
AlertDialogitemList.add(new ALItensValuesList(R.drawable.atrib_utensils, "0i9", "Test 9", "Ut ullamcorper nisi dui, et sagittis ante mollis vitae"));
AlertDialogitemList.add(new ALItensValuesList(R.drawable.atrib_whatsapp, "k10", "Test 10", "Donec rhoncus magna in mauris ultricies"));
// Starting with some values already selected.
// I would like to know a way to simplify this loop.
StringBuilder itensexib = new StringBuilder();
for (int i = 0; i < AlertDialogitemList.size(); i++) {
for (int j = 0; j < selectInitItens.length; j++) {
if (AlertDialogitemList.get(i).getValue().equals(selectInitItens[j])) {
AlertDialogitemList.get(i).setChecked(true);
selectedItemsList.add(AlertDialogitemList.get(i).getValue());
itensexib.append(AlertDialogitemList.get(i).getTitulo()+",");
}
}
}
// Displays the items selected on the button.
Button btnopendialog = findViewById(R.id.btndialog);
if(selectedItemsList.size() >= 1) {
btnopendialog.setText(itensexib);
} else {
btnopendialog.setText("Select Itens");
}
// Creating the dialog
AlertDialog.Builder dialogbuider = new AlertDialog.Builder(this);
dialogbuider.setCancelable(false);
dialogbuider.setTitle("Itens");
ALItensValuesAdapter mAdapter = new ALItensValuesAdapter(AlertDialogitemList, this, new ALItensInterface() {
@Override
public void onItemChecked(int position)
{
AlertDialogitemList.get(position).setChecked(!AlertDialogitemList.get(position).getChecked());
}
});
dialogbuider.setAdapter(mAdapter, (dialog, which) -> {});
dialogbuider.setPositiveButton("OK", (dialogInterface, which) -> {
StringBuilder itensexib2 = new StringBuilder();
selectedItemsList.clear();
for(ALItensValuesList item : AlertDialogitemList) {
if(item.getChecked()) {
selectedItemsList.add(item.getValue());
itensexib2.append(item.getTitulo()+",");
}
}
if(selectedItemsList.size() > 0) {
btnopendialog.setText(itensexib2);
} else {
btnopendialog.setText("Select Itens");
}
});
dialogbuider.setNeutralButton("Clear", (dialogInterface, which) -> {
for (int i = 0; i < AlertDialogitemList.size(); i++){
AlertDialogitemList.get(i).setChecked(false);
}
selectedItemsList.clear();
btnopendialog.setText("Select Itens");
});
AlertDialog dialog = dialogbuider.create();
ListView listView = dialog.getListView();
listView.setDivider(new ColorDrawable(Color.GRAY));
listView.setDividerHeight(2);
btnopendialog.setOnClickListener(v -> {
dialog.show();
});
// Displays the values that will be sent by the form.
findViewById(R.id.btnenviar).setOnClickListener(v -> {
Toast.makeText(this, selectedItemsList.toString(), Toast.LENGTH_LONG).show();
});
}
如果有人知道使该对话框以这种方式工作的其他方法,我很想见面。
答案 0 :(得分:0)
您的逻辑代码保持不变。但是它转移到了Dialog Fragment中,因此活动代码减少了,并且所有与对话框相关的代码也转移到了对话框片段中。
您可以在这里看到简单的DialogFragment代码。
public class SimpleDialogFragment extends DialogFragment {
public static final String TAG = "SimpleDialogFragment";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.dialog_company_insight, container);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setCancelable(false);
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.AppTheme_FullWidthDialog_MoreRounded);
}
}
用于从片段打开对话框:
new SimpleDialogFragment ()
.show(getChildFragmentManager(), SimpleDialogFragment .TAG);
用于从活动中打开对话框:
new SimpleDialogFragment ()
.show(getSupportFragmentManager(), SimpleDialogFragment .TAG);
您可以在此处编码,就像简单的片段一样。如果仍然有任何查询评论,我会看到的。