我想创建一个布局,其中列表视图中需要有多个键值对,用户可以编辑。我想以编程方式创建一个看起来像这样的布局。
[Title] [在列表中添加一行的按钮]
[[EditText for key] [EditText for value]]
[[EditText for key] [EditText for value]]
我是Android开发的新手,所以我不确定在ListView中嵌套EditText视图是否是正确的方法。另外,我使用RubyMotion,它允许我在Ruby中编写应用程序,这就是我想以编程方式执行此操作的原因。
答案 0 :(得分:1)
这是活动
public class MainActivity extends Activity {
private LinearLayout root;
private KeyValueAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* create content view
*/
root = new LinearLayout(this);
root.setOrientation(LinearLayout.VERTICAL);
ViewGroup.LayoutParams rootParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
root.setLayoutParams(rootParams);
setContentView(root);
/**
* create header view (title + button)
*/
LinearLayout header = new LinearLayout(this);
ViewGroup.LayoutParams headerParams = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
header.setLayoutParams(headerParams);
header.setGravity(Gravity.CENTER_VERTICAL);
TextView textView = new TextView(this);
textView.setText("Some title");
header.addView(textView);
Button button = new Button(this);
button.setText("Add row");
header.addView(button);
root.addView(header);
adapter = new KeyValueAdapter(this);
/**
* create list view
*/
ListView listView = new ListView(this);
listView.setLayoutParams(rootParams);
listView.setAdapter(adapter);
root.addView(listView);
/**
* set click listener
*/
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adapter.addRow();
}
});
}
}
适配器实施
static class KeyValueAdapter extends BaseAdapter{
private List<Entry> data;
private Context context;
KeyValueAdapter(Context context) {
data = new ArrayList<>();
this.context = context;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout rootView = new LinearLayout(context);
rootView.setOrientation(LinearLayout.HORIZONTAL);
rootView.setGravity(Gravity.CENTER_VERTICAL);
final Entry entry = data.get(position);
EditText keyEditText = new EditText(context);
keyEditText.setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f));
keyEditText.setHint("Key");
keyEditText.setText(entry.key);
keyEditText.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) {
entry.key = s.toString();
}
@Override
public void afterTextChanged(Editable s) {
}
});
EditText valueEditText = new EditText(context);
valueEditText.setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f));
valueEditText.setHint("Value");
valueEditText.setText(entry.value);
valueEditText.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) {
entry.value = s.toString();
}
@Override
public void afterTextChanged(Editable s) {
}
});
rootView.addView(keyEditText);
rootView.addView(valueEditText);
return rootView;
}
public void addRow(Entry entry){
data.add(0, entry);
notifyDataSetChanged();
}
public void addRow(){
data.add(0, new Entry("", ""));
notifyDataSetChanged();
}
}
作为列表项输入
public static class Entry{
public String key;
public String value;
public Entry(String key, String value) {
this.key = key;
this.value = value;
}
}
我所有这些都在同一个文件中,我为你划分了课程