现在的位置: 首页 > 综合 > 正文

android学习小结5

2012年12月01日 ⁄ 综合 ⁄ 共 7806字 ⁄ 字号 评论关闭

1 用一个VIEW做为消息提示
    btn3.setOnClickListener(new Button.OnClickListener() {
   public void onClick(View v) {
    View view = inflateView(R.layout.view);
    TextView txtMsg = (TextView) view.findViewById(R.id.txtMsg);
    txtMsg.setText("提示内容");

    Toast toast = new Toast(Main.this);
    toast.setView(view);
    toast.setDuration(Toast.LENGTH_LONG);
    toast.show();
   }
  });
2 状态栏通知
   public void onClick(View v) {
    // 实例化通知管理器
    NotificationManager nm = (NotificationManager) getSystemService

(NOTIFICATION_SERVICE);

    // 指定单击通知后所打开的详细的通知页面(单击通知后打开 NotificationView


    PendingIntent contentIntent = PendingIntent.getActivity(
      Main.this, 0, new Intent(Main.this, 

NotificationView.class), 0);

    // 实例化一个通知,并指定其图标和标题(在提示栏上显示)
    Notification n = new Notification(R.drawable.icon01, "我是滚动的通知信息

我是滚动的通知信息我是滚动的通知信息", System.currentTimeMillis());
    
    // 设置通知的发送人和通知的详细内容(打开提示栏后在通知列表中显示)
    n.setLatestEventInfo(Main.this, "通知发送人", "我是详细的通知信息我是详

细的通知信息我是详细的通知信息", contentIntent);

    // 100 毫秒延迟后,震动 250 毫秒,暂停 100 毫秒后,再震动 500 毫秒
    n.vibrate = new long[] { 100, 250, 100, 500 };
    
    // 发出通知(其中第一个参数为通知标识符)
    nm.notify(0, n);
   }
  });
 
3 图片按钮:
    ImageButton imgButton = (ImageButton) this.findViewById(R.id.imageButton);
  // 设置图片按钮的背景
  imgButton.setBackgroundResource(R.drawable.icon01);
4 图片显示:
      ImageView imgView = (ImageView) this.findViewById(R.id.imageView);
  // 指定需要显示的图片
  imgView.setBackgroundResource(R.drawable.icon01);
5 checkbox:
     CheckBox chk = (CheckBox) this.findViewById(R.id.chk1);
  // setOnCheckedChangeListener() - 响应复选框的选中状态改变事件
  chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  
   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    TextView txt = (TextView) _CheckBox.this.findViewById(R.id.textView);
    txt.setText("CheckBox01 的选中状态:" + String.valueOf(isChecked));  

  
   }
  });

6 ToggleButton:
    <!--
        ToggleButton - 双状态按钮控件
            textOn - 当按钮状态为 true 时所显示的文本
            textOff - 当按钮状态为 false 时所显示的文本
    -->
    <ToggleButton android:id="@+id/toggleButton"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:textOn="关闭" android:textOff="打开" />

    final ToggleButton btn = (ToggleButton) this.findViewById(R.id.toggleButton);
        // setOnClickListener() - 响应按钮的鼠标单击事件
        btn.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v) {
                TextView txt = (TextView) _ToggleButton.this.findViewById(R.id.textView);
                // ToggleButton.isChecked() - 双状态按钮的按钮状态
                txt.setText("按钮状态:" + String.valueOf(btn.isChecked()));
            }
        });
7 进度条:
   <!--
        进度条控件(条状)的演示
            style - 进度条的样式,本例使用内置样式
            max - 进度的最大值
            progress - 第一进度位置
            secondaryProgress - 第二进度位置
    -->
    <ProgressBar android:id="@+id/progress_horizontal"
        style="?android:attr/progressBarStyleHorizontal" android:layout_width="200px"
        android:layout_height="wrap_content" android:max="100"
        android:progress="50" android:secondaryProgress="75" />

     // 设置特性以允许在应用程序的标题栏上显示进度条(条状)
        requestWindowFeature(Window.FEATURE_PROGRESS);
        // 设置特性以允许在应用程序的标题栏上显示进度条(圆圈状)
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

        this.setContentView(R.layout.progressbar);

        setTitle("ProgressBar");
       
        // 在标题栏上显示进度条(条状)
        setProgressBarVisibility(true);
        // 在标题栏上显示进度条(圆圈状)
        setProgressBarIndeterminateVisibility(true);
       
        // 指定进度条的进度
        setProgress(50 * 100);
        setSecondaryProgress(75 * 100);

9 SeekBar:
   <!--
  SeekBar - 可拖动的进度条控件
   max - 进度的最大值
   progress - 第一进度位置
   secondaryProgress - 第二进度位置
 -->
 <SeekBar android:id="@+id/seekBar" android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:max="100"
  android:progress="50" android:secondaryProgress="75" />
      
        mSeekBar = (SeekBar) findViewById(R.id.seekBar);
  // setOnSeekBarChangeListener() - 响应拖动进度条事件
  mSeekBar.setOnSeekBarChangeListener(this);
       // 拖动进度条后,进度发生改变时的回调事件
 public void onProgressChanged(SeekBar seekBar, int progress,
   boolean fromTouch) {
  mProgressText.setText(progress + "%");
 }

 // 拖动进度条前开始跟踪触摸
 public void onStartTrackingTouch(SeekBar seekBar) {
  mTrackingText.setText("开始跟踪触摸");
 }

 // 拖动进度条后停止跟踪触摸
 public void onStopTrackingTouch(SeekBar seekBar) {
  mTrackingText.setText("停止跟踪触摸");
 }

10 放大缩小控件:
   ZoomControls zoomControls = (ZoomControls) this.findViewById(R.id.zoomControls);
  // setOnZoomInClickListener() - 响应单击放大按钮的事件
  zoomControls.setOnZoomInClickListener(new OnClickListener() {
   public void onClick(View v) {
    Toast.makeText(_ZoomControls.this, "单击了放大按钮",

Toast.LENGTH_SHORT).show();
   }
  });
  
  // setOnZoomOutClickListener() - 响应单击缩小按钮的事件
  zoomControls.setOnZoomOutClickListener(new OnClickListener() {
   public void onClick(View v) {
    Toast.makeText(_ZoomControls.this, "单击了缩小按钮",

Toast.LENGTH_SHORT).show(); 
   }
  });

11 videoview:
    VideoView videoView = (VideoView) findViewById(R.id.videoView);
       
        // 指定需要播放的视频的地址
        videoView.setVideoURI(Uri.parse("android.resource://com.webabcd.view/" + R.raw.demo));
        // videoView.setVideoPath();
        
        // 设置播放器的控制条
        videoView.setMediaController(new MediaController(this));
        // 开始播放视频
        videoView.start();

12 tab控件:
   <!-- Tab 1 的内容 -->
 <TextView android:id="@+id/view1" android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:text="tab1 content" />
  
 <!-- Tab 2 的内容 -->
 <TextView android:id="@+id/view2" android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:text="tab2 content" />

// 实现 Tab 功能的话要继承 TabActivity
public class _Tab extends TabActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);

  TabHost tabHost = getTabHost();
  LayoutInflater.from(this).inflate(R.layout.tab, tabHost.getTabContentView(), true);

  // Tab 1 的内容
  tabHost.addTab(tabHost.newTabSpec("tab1")
    .setIndicator("tab1")
    .setContent(R.id.view1));
  
  // Tab 2 的内容(设置了 Tab 图片)
  tabHost.addTab(tabHost.newTabSpec("tab2")
    .setIndicator("tab2", getResources().getDrawable(R.drawable.icon01))
    .setContent(R.id.view2));
  
  // Tab 3 的内容(设置 Tab 的内容为指定的 Activity)
  tabHost.addTab(tabHost.newTabSpec("tab3")
    .setIndicator("tab3")
    .setContent(new Intent(this, _TextView.class)));

 }
}

13 GALLERY缩略图组件:
    <!--
        Gallery - 缩略图浏览器控件
            spacing - 缩略图列表中各个缩略图之间的间距
    -->
    <Gallery android:id="@+id/gallery" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:spacing="20px" />

  Gallery gallery = (Gallery) findViewById(R.id.gallery);
        // 为缩略图浏览器指定一个适配器
        gallery.setAdapter(new ImageAdapter(this));
        // 响应 在缩略图列表上选中某个缩略图后的 事件
        gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View v,
                    int position, long id) {
                Toast.makeText(_Gallery.this, String.valueOf(position), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {

            }
        });
    }

    // 继承 BaseAdapter 用以实现自定义的图片适配器
    public class ImageAdapter extends BaseAdapter {

        private Context mContext;

        public ImageAdapter(Context context) {
            mContext = context;
        }

        public int getCount() {
            return mThumbIds.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView image = new ImageView(mContext);

            image.setImageResource(mThumbIds[position]);
            image.setAdjustViewBounds(true);
            image.setLayoutParams(new Gallery.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

            return image;
        }
    }

    // 需要显示的图片集合
    private Integer[] mThumbIds = { R.drawable.icon01, R.drawable.icon02,
            R.drawable.icon03, R.drawable.icon04, R.drawable.icon05 };

抱歉!评论已关闭.