我正在尝试使用可编辑的行创建 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();
}
}
自定义 ArrayAdapter
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?)一起使用。
谁能告诉如何使它正常工作?