Android学习08-----Android中的基本控件(下)(4)_Toast ImageSwitcher TextSwitcher
程序员文章站
2022-05-22 17:34:09
...
一、信息提示框: Toast
1 、提示文字信息
Main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/butA" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="长时间显示Toast" /> <Button android:id="@+id/butB" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="短时间显示Toast" /> </LinearLayout>
Toast01Activity.java
package com.iflytek.demo; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class Toast01Activity extends Activity { private Button butA = null; private Button butB = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.butA = (Button) super.findViewById(R.id.butA); this.butB = (Button) super.findViewById(R.id.butB); this.butA.setOnClickListener(new OnClickListenerImplLong()); this.butB.setOnClickListener(new OnClickListenerImplShort()); } private class OnClickListenerImplLong implements OnClickListener { @Override public void onClick(View v) { Toast.makeText(Toast01Activity.this, "长时间显示的Toast信息提示框", Toast.LENGTH_LONG).show(); } } private class OnClickListenerImplShort implements OnClickListener { @Override public void onClick(View v) { Toast.makeText(Toast01Activity.this, "短时间显示的Toast信息提示框", Toast.LENGTH_SHORT).show(); } } }
2 、自定义显示风格
Toast02Activity.java
package com.iflytek.demo; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.Toast; public class Toast02Activity extends Activity { private Button but = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.but = (Button) super.findViewById(R.id.but); this.but.setOnClickListener(new OnClickListenerImpl()); } private class OnClickListenerImpl implements OnClickListener { @Override public void onClick(View v) { Toast myToast = Toast.makeText(Toast02Activity.this, "安徽合肥", Toast.LENGTH_LONG); myToast.setGravity(Gravity.CENTER, 60, 30); LinearLayout myToastView = (LinearLayout) myToast.getView(); // 线性布局 ImageView img = new ImageView(Toast02Activity.this); img.setImageResource(R.drawable.ic_launcher); myToastView.addView(img, 0); // 放在最前面 myToast.show(); } } }
二、图片切换: ImageSwitcher
Main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageSwitcher android:id="@+id/myImageSwitcher" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <Button android:id="@+id/butPrevious" android:layout_width="wrap_content" android:layout_height="wrap_content" android:enabled="false" android:text="上一张图片" /> <Button android:id="@+id/butNext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:enabled="true" android:text="下一张图片" /> </LinearLayout> </LinearLayout>
ImageSwitcherActivity.java
package com.iflytek.demo; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.view.animation.AnimationUtils; import android.widget.Button; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.ViewSwitcher.ViewFactory; public class ImageSwitcherActivity extends Activity { private Button butPrevious = null; private Button butNext = null; private ImageSwitcher myImageSwitcher = null; private int[] imgRes = new int[] { R.drawable.ispic_a, R.drawable.ispic_b, R.drawable.ispic_c, R.drawable.ispic_d, R.drawable.ispic_e }; private int foot = 0; // 表示当前已经显示的数组图片的脚标 /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.myImageSwitcher = (ImageSwitcher) super .findViewById(R.id.myImageSwitcher); this.butPrevious = (Button) super.findViewById(R.id.butPrevious); this.butNext = (Button) super.findViewById(R.id.butNext); this.myImageSwitcher.setFactory(new ViewFactoryImpl()); this.myImageSwitcher.setImageResource(this.imgRes[this.foot++]); this.myImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); this.myImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); this.butPrevious.setOnClickListener(new OnClickListenerPrevious()); this.butNext.setOnClickListener(new OnClickListenerNext()); } private class OnClickListenerPrevious implements OnClickListener { @Override public void onClick(View view) { ImageSwitcherActivity.this.myImageSwitcher .setImageResource(ImageSwitcherActivity.this.imgRes[ImageSwitcherActivity.this.foot--]); ImageSwitcherActivity.this.checkButEnable(); } } private class OnClickListenerNext implements OnClickListener { @Override public void onClick(View view) { ImageSwitcherActivity.this.myImageSwitcher .setImageResource(ImageSwitcherActivity.this.imgRes[ImageSwitcherActivity.this.foot++]); ImageSwitcherActivity.this.checkButEnable(); } } private void checkButEnable() { // 判断按钮是否可用 if (this.foot < this.imgRes.length - 1) { this.butNext.setEnabled(true); } else { this.butNext.setEnabled(false); } if (this.foot == 0) { this.butPrevious.setEnabled(false); } else { this.butPrevious.setEnabled(true); } } private class ViewFactoryImpl implements ViewFactory { @Override public View makeView() { ImageView img = new ImageView(ImageSwitcherActivity.this); img.setBackgroundColor(0xFFFFFFFF); // 设置背景 img.setScaleType(ImageView.ScaleType.CENTER); img.setLayoutParams(new ImageSwitcher.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); // 定义组件 return img; } } }
三、文本切换: TextSwitcher
Main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextSwitcher android:id="@+id/myTextSwitcher" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/but" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示当前时间" /> </LinearLayout>
TextSwitcherActivity.java
package com.iflytek.demo; import java.text.SimpleDateFormat; import java.util.Date; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.view.animation.AnimationUtils; import android.widget.Button; import android.widget.TextSwitcher; import android.widget.TextView; import android.widget.ViewSwitcher.ViewFactory; public class TextSwitcherActivity extends Activity { private TextSwitcher myTextSwitcher = null; private Button but = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.myTextSwitcher = (TextSwitcher) super .findViewById(R.id.myTextSwitcher); this.but = (Button) super.findViewById(R.id.but); this.myTextSwitcher.setFactory(new ViewFactoryImpl()); this.myTextSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); this.myTextSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); this.but.setOnClickListener(new OnClickListenerImpl()); } private class OnClickListenerImpl implements OnClickListener { @Override public void onClick(View v) { TextSwitcherActivity.this.myTextSwitcher.setText("当前时间为:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") .format(new Date())); } } private class ViewFactoryImpl implements ViewFactory { @Override public View makeView() { TextView txt = new TextView(TextSwitcherActivity.this); txt.setBackgroundColor(0xFFFFFFFF); txt.setTextColor(0xFF000000); txt.setLayoutParams(new TextSwitcher.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); txt.setTextSize(30); return txt; } } }
四、拖拉图片: Gallery
1 、 BaseAdapter 实现
Gallery01_BaseAdapterActivity.java
package com.iflytek.demo; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Gallery; import android.widget.Toast; public class Gallery01_BaseAdapterActivity extends Activity { private Gallery gallery = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.gallery = (Gallery) super.findViewById(R.id.myGallery); this.gallery.setAdapter(new ImageGalleryAdapter(this)); this.gallery.setOnItemClickListener(new OnItemClickListenerImpl()) ; } private class OnItemClickListenerImpl implements OnItemClickListener { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(Gallery01_BaseAdapterActivity.this, String.valueOf(position), Toast.LENGTH_SHORT).show(); } } }
ImageGalleryAdapter.java
package com.iflytek.demo; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; public class ImageGalleryAdapter extends BaseAdapter { private Context context = null; // 里面的所有方法表示的是可以根据指定的显示图片的数量,进行每个图片的处理 private int[] imgRes = new int[] { R.drawable.ispic_a, R.drawable.ispic_b, R.drawable.ispic_c, R.drawable.ispic_d, R.drawable.ispic_e }; // 这些是所要显示的图片的资源 public ImageGalleryAdapter(Context context) { this.context = context; } @Override public int getCount() { // 取得要显示的内容的数量 return this.imgRes.length; // 资源的数量 } @Override public Object getItem(int position) { return this.imgRes[position]; } @Override public long getItemId(int position) { // 取得项的ID return this.imgRes[position]; } // 将资源设置到一个组件之中,很明显这个组件就是ImageView组件 @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView img = new ImageView(this.context); img.setBackgroundColor(0xFFFFFFFF); img.setImageResource(this.imgRes[position]); // 将指定的资源设置到ImageView中 img.setScaleType(ImageView.ScaleType.CENTER); // 居中显示 img.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); return img; } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Gallery android:id="@+id/myGallery" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:spacing="3px" /> </LinearLayout>
2 、 SimpleAdapter 实现
grid_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#FFFFFF"> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="center"/> </LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Gallery android:id="@+id/myGallery" android:gravity="center_vertical" android:spacing="3px" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
Gallery02_SimpleAdapterActivity.java
package com.iflytek.demo; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Gallery; import android.widget.SimpleAdapter; import android.widget.Toast; public class Gallery02_SimpleAdapterActivity extends Activity { private Gallery gallery = null; private SimpleAdapter simpleAdapter = null; private List<Map<String, Integer>> list = new ArrayList<Map<String, Integer>>(); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.initAdapter(); this.gallery = (Gallery) super.findViewById(R.id.myGallery); this.gallery.setAdapter(this.simpleAdapter); this.gallery.setOnItemClickListener(new OnItemClickListenerImpl()); } private void initAdapter() { Field[] fields = R.drawable.class.getDeclaredFields(); // 取得全部的属性 for (int x = 0; x < fields.length; x++) { if (fields[x].getName().startsWith("ispic_")) { // 我们所需要的图片 Map<String, Integer> map = new HashMap<String, Integer>(); try { map.put("img", fields[x].getInt(R.drawable.class)); // 必须定义好名称是img } catch (Exception e) { } this.list.add(map); } } this.simpleAdapter = new SimpleAdapter(this, this.list, R.layout.grid_layout, new String[] { "img" }, new int[] { R.id.img }); } private class OnItemClickListenerImpl implements OnItemClickListener { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(Gallery02_SimpleAdapterActivity.this, String.valueOf(position), Toast.LENGTH_SHORT).show(); } } }
3 、图片浏览
Gallery03_Activity.java
package com.iflytek.demo; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup.LayoutParams; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Gallery; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.SimpleAdapter; import android.widget.ViewSwitcher.ViewFactory; public class Gallery03_Activity extends Activity { private Gallery gallery = null; private SimpleAdapter simpleAdapter = null; private List<Map<String, Integer>> list = new ArrayList<Map<String, Integer>>(); private ImageSwitcher myImageSwitcher = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.initAdapter(); this.gallery = (Gallery) super.findViewById(R.id.myGallery); this.myImageSwitcher = (ImageSwitcher) super .findViewById(R.id.myImageSwitcher); this.gallery.setAdapter(this.simpleAdapter); this.myImageSwitcher.setFactory(new ViewFactoryImpl()); this.gallery.setOnItemClickListener(new OnItemClickListenerImpl()); } private void initAdapter() { Field[] fields = R.drawable.class.getDeclaredFields(); // 取得全部的属性 for (int x = 0; x < fields.length; x++) { if (fields[x].getName().startsWith("ispic_")) { // 我们所需要的图片 Map<String, Integer> map = new HashMap<String, Integer>(); try { map.put("img", fields[x].getInt(R.drawable.class)); // 必须定义好名称是img } catch (Exception e) { } this.list.add(map); } } this.simpleAdapter = new SimpleAdapter(this, this.list, R.layout.grid_layout, new String[] { "img" }, new int[] { R.id.img }); } private class OnItemClickListenerImpl implements OnItemClickListener { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Map<String, Integer> map = (Map<String, Integer>) parent .getAdapter().getItem(position); Gallery03_Activity.this.myImageSwitcher.setImageResource(map.get("img")); } } private class ViewFactoryImpl implements ViewFactory { @Override public View makeView() { ImageView img = new ImageView(Gallery03_Activity.this); img.setBackgroundColor(0xFFFFFFFF); img.setScaleType(ImageView.ScaleType.CENTER); img.setLayoutParams(new ImageSwitcher.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); return img; } } }
grid_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#FFFFFF"> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaleType="center"/> </LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageSwitcher android:id="@+id/myImageSwitcher" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Gallery android:id="@+id/myGallery" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:spacing="3px" /> </LinearLayout>