Android ApiDemos示例解析(179):Views->Lists->12. Transcript
本例使用一个ListView 和一个TextView 实现了一个类似便笺簿功能,在TextView中输入,然后在ListView中显示,使用ArrayAdapter, 字符串存储使用ArrayList.
[java]
private ArrayAdapter<String> mAdapter;
private ArrayList<String> mStrings = new ArrayList<String>();
...
mAdapter = new ArrayAdapter<String>(this,
android.R.layout.易做图_list_item_1, mStrings);
setListAdapter(mAdapter);
private ArrayAdapter<String> mAdapter;
private ArrayList<String> mStrings = new ArrayList<String>();
...
mAdapter = new ArrayAdapter<String>(this,
android.R.layout.易做图_list_item_1, mStrings);
setListAdapter(mAdapter);
为EditText 添加Click 和Key 事件处理,在点击或是按“中键”时将输入文字中ListView中添加一行:
[java]
public void onClick(View v) {
sendText();
}
private void sendText() {
String text = mUserText.getText().toString();
mAdapter.add(text);
mUserText.setText(null);
}
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
sendText();
return true;
}
}
return false;
}
public void onClick(View v) {
sendText();
}
private void sendText() {
String text = mUserText.getText().toString();
mAdapter.add(text);
mUserText.setText(null);
}
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
sendText();
return true;
}
}
return false;
}
补充:移动开发 , Android ,