1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.apis.view; 18 19 import android.app.ListActivity; 20 import android.os.Bundle; 21 import android.view.KeyEvent; 22 import android.view.View; 23 import android.view.View.OnClickListener; 24 import android.view.View.OnKeyListener; 25 import android.widget.ArrayAdapter; 26 import android.widget.EditText; 27 28 import com.example.android.apis.R; 29 30 import java.util.ArrayList; 31 32 /** 33 * Demonstrates the using a list view in transcript mode 34 * 35 */ 36 public class List12 extends ListActivity implements OnClickListener, OnKeyListener { 37 38 private EditText mUserText; 39 40 private ArrayAdapter<String> mAdapter; 41 42 private ArrayList<String> mStrings = new ArrayList<String>(); 43 44 @Override 45 protected void onCreate(Bundle savedInstanceState) { 46 super.onCreate(savedInstanceState); 47 48 setContentView(R.layout.list_12); 49 50 mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings); 51 52 setListAdapter(mAdapter); 53 54 mUserText = (EditText) findViewById(R.id.userText); 55 56 mUserText.setOnClickListener(this); 57 mUserText.setOnKeyListener(this); 58 } 59 60 public void onClick(View v) { 61 sendText(); 62 } 63 64 private void sendText() { 65 String text = mUserText.getText().toString(); 66 mAdapter.add(text); 67 mUserText.setText(null); 68 } 69 70 public boolean onKey(View v, int keyCode, KeyEvent event) { 71 if (event.getAction() == KeyEvent.ACTION_DOWN) { 72 switch (keyCode) { 73 case KeyEvent.KEYCODE_DPAD_CENTER: 74 case KeyEvent.KEYCODE_ENTER: 75 sendText(); 76 return true; 77 } 78 } 79 return false; 80 } 81 82 83 } 84