簡體   English   中英

帶有可編輯列表視圖的 android 對話框

[英]android dialog with editable listview

我正在嘗試創建一個帶有可編輯行的 ListView:

主要活動

public class AlertDialogList extends AppCompatActivity {
protected final String DEBUG_TAG = this.getClass().getName();
List<Pair<String, String>> headers = new LinkedList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    headers.add(new Pair<String, String>("Event", "?"));
    headers.add(new Pair<String, String>("Site", "?"));
    headers.add(new Pair<String, String>("Date", "?"));
    headers.add(new Pair<String, String>("Round", "?"));
    headers.add(new Pair<String, String>("White", "?"));
    headers.add(new Pair<String, String>("Black", "?"));
    headers.add(new Pair<String, String>("Result", "?"));
    headers.add(new Pair<String, String>("WhiteElo", "?"));
    headers.add(new Pair<String, String>("BlackElo", "?"));
    headers.add(new Pair<String, String>("ECO", "?"));

    RelativeLayout layout = new RelativeLayout(this);
    RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    layout.setBackgroundColor(Color.GREEN);
    this.setContentView(layout, rlp);

    Button button = new Button(this);
    button.setText("Show Alert Dialog");
    button.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    layout.addView(button);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            launchDialog();
        }
    });
}

public void launchDialog() {
    final AlertDialog.Builder builder = new AlertDialog.Builder(AlertDialogList.this);
    ArrayAdapter arrayAdapter = new CPHeaderListAdapter(this, headers);
    builder.setSingleChoiceItems(arrayAdapter, -1, null);

    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            Log.d(DEBUG_TAG, "PositiveButton onClick()");
        }
    });

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

自定義陣列適配器

public class CPHeaderListAdapter extends ArrayAdapter<Object> {
protected final String DEBUG_TAG = this.getClass().getName();
private List<Pair<String, String>> headerList;
private LayoutInflater layoutInflater;

public CPHeaderListAdapter(Context context, @NonNull List<Pair<String, String>> headers) {
    super(context, R.layout.list_view, R.id.headerValue);
    headerList = headers;
    layoutInflater = LayoutInflater.from(context.getApplicationContext());
}

@Override
public int getCount() {
    return headerList.size();
}

@Override
public Object getItem(int position) {
    return headerList.get(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    RowViewHolder rowViewHolder;
    if (convertView == null) {
        convertView = layoutInflater.inflate(R.layout.list_view, null);
        rowViewHolder = new RowViewHolder();
        convertView.setTag(rowViewHolder);
        LinearLayout layout = convertView.findViewById(R.id.headerRowLayout);
        layout.setVisibility(View.VISIBLE);
        rowViewHolder.labelView = convertView.findViewById(R.id.headerLabel);
        rowViewHolder.valueView = convertView.findViewById(R.id.headerValue);
        rowViewHolder.actionButton = convertView.findViewById(R.id.headerActionButton);
    } else {
        rowViewHolder = (RowViewHolder) convertView.getTag();
    }

    parent.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);

    rowViewHolder.valueView.setTag(position);
    rowViewHolder.actionButton.setTag(position);
    Pair<String, String> header = headerList.get(position);
    rowViewHolder.labelView.setText(header.first);
    rowViewHolder.valueView.setText(header.second);
    rowViewHolder.valueView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return false;
        }
    });

    rowViewHolder.actionButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int position = Integer.valueOf(v.getTag().toString());
            Log.d(DEBUG_TAG, String.format("onClick actionButton %s, #%d", v.getTag().toString(), position));
        }
    });

    rowViewHolder.valueView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
                int position = Integer.valueOf(v.getTag().toString());
                Pair<String, String> header = headerList.get(position);
                String label = header.first;
                String text = ((TextView) v).getText().toString();
                Pair<String, String> newHeader = new Pair<>(label, text);
                headerList.set(position, newHeader);
            }
        }
    });
    return convertView;
}

private class RowViewHolder {
    TextView labelView;
    TextView valueView;
    Button actionButton;
}
}

布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
>

<LinearLayout
    android:id="@+id/headerRowLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginLeft="10sp"
    android:layout_marginRight="10sp"
    >

    <TextView
        android:id="@+id/headerLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:textColor="#505050"
        />

    <EditText
        android:id="@+id/headerValue"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="text"
        android:scrollHorizontally="true"
        android:textColor="#000000"
        />

    <Button android:id="@+id/headerActionButton"
        android:text="del"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        />

</LinearLayout>


它有一個主要缺陷——當我點擊一個顯示“?”的字段時,Android 不會彈出鍵盤:
在此處輸入圖片說明

為了解決這個問題,我在之前添加了一個不可見的 EditText

 AlertDialog dialog = builder.create();

線:

    EditText editText = new EditText(AlertDialogList.this.getApplicationContext());
editText.setVisibility(View.GONE);
builder.setView(editText);


現在所有行都按預期工作:
在此處輸入圖片說明
但是底部的“確定”按鈕消失了(如果屏幕足夠小):
在此處輸入圖片說明
有趣的是,它曾經適用於 AndroidStudio 2 和更舊的 SDK(21?)。
誰能告訴如何使它正常工作?

您可以使用此代碼隨心所欲地創建AlertDialog 將您的 ListView 放在 layout/your_layout_xml 上,並從dialogView以編程方式訪問它們並照常使用。

    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());

    LayoutInflater inflater = getActivity().getLayoutInflater();
    final View dialogView = inflater.inflate(R.layout.your_layout_xml, null);
    dialogBuilder.setView(dialogView);

    final ListView listView= dialogView.findViewById(R.id.listView); 

    dialogBuilder.setTitle(getString(R.string.app_name));
    dialogBuilder.setIcon(R.mipmap.ic_launcher);
    dialogBuilder.setCancelable(true);

    dialogBuilder.setMessage(getString(R.string.app_dialog_title));


    dialogBuilder.setPositiveButton(getString(R.string.dialog_ok), new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) { 
            //your code 
        }
    });


    dialogBuilder.setNeutralButton(R.string.dialog_cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            dialog.dismiss(); 
        }
    });

    AlertDialog b = dialogBuilder.create();
    b.show();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM