EX04_19.java
上传用户:vip_99
上传日期:2021-03-27
资源大小:61159k
文件大小:3k
源码类别:

android开发

开发平台:

Java

  1. package irdc.ex04_19;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.AdapterView;
  6. import android.widget.ArrayAdapter;
  7. import android.widget.LinearLayout;
  8. import android.widget.ListView;
  9. import android.widget.TextView;
  10. public class EX04_19 extends Activity
  11. {
  12.   private static final String[] array =
  13.   {
  14.     "sunday", "monday", "tuesday",
  15.     "wednesday", "thursday", "friday",
  16.     "saturday"
  17.   };
  18.   
  19.   LinearLayout myLinearLayout;
  20.   TextView myTextView;
  21.   ListView myListView;
  22.   
  23.   /** Called when the activity is first created. */
  24.   @Override
  25.   public void onCreate(Bundle savedInstanceState)
  26.   {
  27.     super.onCreate(savedInstanceState);
  28.     /* 添加LinearLayout */
  29.     myLinearLayout = new LinearLayout(this);
  30.     myLinearLayout.setOrientation(LinearLayout.VERTICAL);
  31.     myLinearLayout.setBackgroundColor(android.graphics.Color.WHITE);
  32.     /* 添加TextView */
  33.     myTextView = new TextView(this);
  34.     LinearLayout.LayoutParams param1 = new LinearLayout.LayoutParams
  35.     (
  36.       LinearLayout.LayoutParams.FILL_PARENT,
  37.       LinearLayout.LayoutParams.WRAP_CONTENT
  38.     );
  39.     
  40.     myTextView.setText(R.string.title);
  41.     
  42.     myTextView.setTextColor(getResources().getColor(R.drawable.blue));
  43.     
  44.     /* 将TextView加到myLinearLayout */
  45.     myLinearLayout.addView(myTextView, param1);
  46.     
  47.     /* 添加ListView */
  48.     myListView = new ListView(this);
  49.     LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams
  50.     (
  51.       LinearLayout.LayoutParams.FILL_PARENT,
  52.       LinearLayout.LayoutParams.WRAP_CONTENT
  53.     );
  54.     
  55.     myListView.setBackgroundColor
  56.     (getResources().getColor(R.drawable.ltgray));
  57.     
  58.     /* 将ListView加到myLinearLayout */
  59.     myLinearLayout.addView(myListView, param2);
  60.     
  61.     /* 将LinearLayout加到ContentView */
  62.     setContentView(myLinearLayout);
  63.     
  64.     /* new ArrayAdapter对象并将array字符串数组传入 */
  65.     ArrayAdapter<String> adapter = 
  66.     new ArrayAdapter<String>
  67.     (this, R.layout.my_simple_list_item, array);
  68.     
  69.     /* 将ArrayAdapter添加ListView对象中 */
  70.     myListView.setAdapter(adapter);
  71.     /* myListView添加OnItemSelectedListener */
  72.     myListView.setOnItemSelectedListener
  73.     (new AdapterView.OnItemSelectedListener()
  74.     {
  75.       @Override
  76.       public void onItemSelected
  77.       (AdapterView<?> arg0, View arg1, int arg2, long arg3)
  78.       {
  79.         /* 使用getSelectedItem()将选择的值带入myTextView中 */
  80.         myTextView.setText
  81.         ("你选的是" + arg0.getSelectedItem().toString());
  82.       }
  83.       
  84.       @Override
  85.       public void onNothingSelected(AdapterView<?> arg0)
  86.       {
  87.         // TODO Auto-generated method stub
  88.       }
  89.     });
  90.     
  91.     /* myListView添加OnItemClickListener */
  92.     myListView.setOnItemClickListener
  93.     (new AdapterView.OnItemClickListener()
  94.     {
  95.       @Override
  96.       public void onItemClick
  97.       (AdapterView<?> arg0, View arg1, int arg2, long arg3)
  98.       {
  99.         /* 使用String[index],arg2为点击到ListView的index,
  100.         并将值带入myTextView中 */
  101.         myTextView.setText("你选的是" + array[arg2]);
  102.       }
  103.     });
  104.   }
  105. }