简易的按钮事件
Button 事件处理
activity_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" > <TextView android:id="@+id/show_TextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/Click_Button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击" /> </LinearLayout>
MainActivity.java
package com.example.demo; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { TextView show; Button press; int i = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); show = (TextView) findViewById(R.id.show_TextView); press = (Button) findViewById(R.id.Click_Button); press.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { i++; if (i % 2 == 0) { show.setText("Hi , Google Android!"); } else { show.setText("Hi , Michael!"); } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
手机页面的转换
setContentView 的应用
activity_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:background="@color/black" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="2" android:orientation="vertical" > <TextView android:id="@+id/text1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/layout1" android:textSize="24sp" > </TextView> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="horizontal" > <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Go to Layout2" > </Button> </LinearLayout> </LinearLayout>
mylayout.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:background="@color/white" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="2" android:orientation="vertical" > <TextView android:id="@+id/text2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/layout2" android:textColor="@color/black" android:textSize="24sp" > </TextView> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="horizontal" > <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Go to Layout1" > </Button> </LinearLayout> </LinearLayout>
string_color.xml
<string name="layout1">this is Layout 1</string> <string name="layout2">This is Layout 2</string> <color name="black">#000000</color> <color name="white">#FFFFFFFF</color>
MainActivity.java
package com.example.demo; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /* 以findViewById()取得Button 对象,并添加onClickListener */ Button b1 = (Button) findViewById(R.id.button1); b1.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { jumpToLayout2();// 调用跳转方法jumpToLayout2() } }); } /* * method jumpToLayout2:将 layout 由 main.xml 切 换 成 mylayout.xml */ public void jumpToLayout2() { /* 将layout 改成mylayout.xml */ setContentView(R.layout.mylayout); /* 以findViewById()取得Button 对象,并添加onClickListener */ Button b2 = (Button) findViewById(R.id.button2); b2.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { jumpToLayout1();// 调用跳转方法jumpToLayout1() } }); } /* * method jumpToLayout1:将 layout 由 mylayout.xml 切 换 成main.xml */ public void jumpToLayout1() { /* 将layout 改成main.xml */ setContentView(R.layout.activity_main); /* 以findViewById()取得Button 对象,并添加onClickListener */ Button b1 = (Button) findViewById(R.id.button1); b1.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { jumpToLayout2();// 调用跳转方法jumpToLayout2() } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
调用另一个Activity
Intent 对象的使用
string_color.xml
<string name="act1">This is Activity 1!</string> <string name="act2">This is Activity 2!</string> <color name="black">#000000</color> <color name="white">#FFFFFFFF</color>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/black" > <TextView android:id="@+id/text1" android:layout_width="186px" android:layout_height="29px" android:layout_x="70px" android:layout_y="32px" android:text="@string/act1" android:textSize="24sp" > </TextView> <Button android:id="@+id/button1" android:layout_width="118px" android:layout_height="wrap_content" android:layout_x="100px" android:layout_y="82px" android:text="Go to Activity2" > </Button> </AbsoluteLayout>
secondlayout.xml
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/white" > <TextView android:id="@+id/text2" android:layout_width="186px" android:layout_height="29px" android:layout_x="70px" android:layout_y="32px" android:text="@string/act2" android:textColor="@color/black" android:textSize="24sp" > </TextView> <Button android:id="@+id/button2" android:layout_width="118px" android:layout_height="wrap_content" android:layout_x="100px" android:layout_y="82px" android:text="Go to Activity1" > </Button> </AbsoluteLayout>
MainActivity.java
package com.example.demo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* 载入main.xml Layout */ setContentView(R.layout.activity_main); /* 以findViewById()取得Button 对象,并添加onClickListener */ Button b1 = (Button) findViewById(R.id.button1); b1.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { /* new 一个Intent 对象,并指定要启动的class */ Intent intent = new Intent(); intent.setClass(MainActivity.this, SecondActivity.class); /* 调用一个新的Activity */ startActivity(intent); /* 关闭原本的Activity */ MainActivity.this.finish(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
SecondActivity.java
package com.example.demo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class SecondActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* 载入mylayout.xml Layout */ setContentView(R.layout.secondlayout); /* 以findViewById()取得Button 对象,并添加onClickListener */ Button b2 = (Button) findViewById(R.id.button2); b2.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { /* new 一个Intent 对象,并指定要启动的class */ Intent intent = new Intent(); intent.setClass(SecondActivity.this, MainActivity.class); /* 调用一个新的Activity */ startActivity(intent); /* 关闭原本的Activity */ SecondActivity.this.finish(); } }); } }
在AndroidManifest.xml 文件中添加SecondActivity
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.demo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="SecondActivity" > </activity> </application> </manifest>
专业相框设计:
ImageView 的堆叠应用
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/widget34" android:layout_width="fill_parent" android:layout_height="fill_parent" > <!-- 创建第一个ImageView (第二层图片) --> <ImageView android:id="@+id/myImageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="0px" android:layout_y="36px" /> <!-- 创建第二个ImageView (第一层图片) --> <ImageView android:id="@+id/myImageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="0px" android:layout_y="36px" /> <!-- 创建第一个Button --> <Button android:id="@+id/myButton1" android:layout_width="105px" android:layout_height="66px" android:layout_x="9px" android:layout_y="356px" myButton1="" android:text="pic1" /> <!-- 创建第二个Button --> <Button android:id="@+id/myButton2" android:layout_width="105px" android:layout_height="66px" android:layout_x="179px" android:layout_y="356px" android:text="pic2" /> </AbsoluteLayout>
MainActivity.java
package com.example.demo; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity { /** Called when the activity is first created. */ /* 声明Button、ImageView 对象 */ private ImageView mImageView01; private ImageView mImageView02; private Button mButton01; private Button mButton02; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /* 取得Button、ImageView 对象 */ mImageView01 = (ImageView) findViewById(R.id.myImageView1); mImageView02 = (ImageView) findViewById(R.id.myImageView2); mButton01 = (Button) findViewById(R.id.myButton1); mButton02 = (Button) findViewById(R.id.myButton2); /* 设置ImageView 背景图 */ mImageView01.setImageDrawable(getResources().getDrawable( R.drawable.right)); mImageView02.setImageDrawable(getResources().getDrawable( R.drawable.photo)); /* 用OnClickListener 事件来启动 */ mButton01.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { /* 当启动后, ImageView 立刻换背景图 */ mImageView01.setImageDrawable(getResources().getDrawable( R.drawable.right)); } }); mButton02.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { mImageView01.setImageDrawable(getResources().getDrawable( R.drawable.left)); } }); } }
相簿浏览Gallery
Gallery 与衍生BaseAdapter 容器
strings.xml
<declare-styleable name="Gallery"> <attr name="android:galleryItemBackground" /> </declare-styleable> <!-- 定义layout 外部resource 的xml 文件,用来改变layout 的背景图。 -->
修改activity_main.xml布局,添加一个Gallery 和一个ImageView
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/widget_absolutelayout" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Gallery android:id="@+id/Gallery_preView" android:layout_width="fill_parent" android:layout_height="143px" android:layout_x="0px" android:layout_y="51px" > </Gallery> <ImageView android:id="@+id/ImageView_photo" android:layout_width="239px" android:layout_height="218px" android:layout_x="38px" android:layout_y="184px" > </ImageView> </AbsoluteLayout>
新建一个myImageAdapter 类--Gallery 的适配器,它继承于BaseAdapter 类.实现相簿浏览效果
package com.example.demo; import android.content.Context; import android.content.res.TypedArray; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; public class myImageAdapter extends BaseAdapter { /* 变量声明 */ int mGalleryItemBackground; private Context context;// 上下文 /* 构建一Integer array 并取得预加载Drawable 的图片id */ public Integer[] myImageIds = { R.drawable.photo1, R.drawable.photo2, R.drawable.photo3, R.drawable.photo4, R.drawable.photo5, R.drawable.photo6, }; /* 自定义的构造方法 */ public myImageAdapter(Context context) { // TODO Auto-generated constructor stub this.context = context; /* * 使用在res/values/attrs.xml 中的<declare-styleable>定义的Gallery 属性. */ TypedArray typed_array = context .obtainStyledAttributes(R.styleable.Gallery); /* 取得Gallery 属性的Index id */ mGalleryItemBackground = typed_array.getResourceId( R.styleable.Gallery_android_galleryItemBackground, 0); /* 让对象的styleable 属性能够反复使用 */ typed_array.recycle(); }/* * 重 写 的 方 法 getCount,返 回 图 片 数 目 */ @Override public int getCount() { // TODO Auto-generated method stub return myImageIds.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } /* * 重 写 的 方 法 getView,返 回 一 View 对 象 */ @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub /* 产生ImageView 对象 */ ImageView imageview = new ImageView(context); /* 设置图片给imageView 对象 */ imageview.setImageResource(myImageIds[position]);/* 重新设置图片的宽高 */ imageview.setScaleType(ImageView.ScaleType.FIT_XY); /* 重新设置Layout 的宽高 */ imageview.setLayoutParams(new Gallery.LayoutParams(128, 128)); /* 设置Gallery 背景图 */ imageview.setBackgroundResource(mGalleryItemBackground); /* 返回imageView 对象 */ return imageview; } }
修改mainActivity.java,添加Gallery 相关操作
package com.example.demo; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.Gallery; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { /** Called when the activity is first created. */ /* 定义要使用的对象 */ private Gallery gallery; private ImageView imageview; private myImageAdapter imageadapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageadapter = new myImageAdapter(this); /* 通过findViewById 取得资源对象 */ gallery = (Gallery) findViewById(R.id.Gallery_preView); imageview = (ImageView) findViewById(R.id.ImageView_photo); /* 给Gallery设置适配器把Ex_Ctrl_10ME类传入参数 */ gallery.setAdapter(imageadapter); /* 设置Gallery的点击事件监听器 */ gallery.setOnItemClickListener(new Gallery.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View v, int position, long id) { // TODO Auto-generated method stub /* 显示该图片是几号 */ Toast.makeText(MainActivity.this, "这是图片:" + position + "号", Toast.LENGTH_SHORT).show(); /* 设置大图片 */ imageview .setBackgroundResource(imageadapter.myImageIds[position]); } }); } }
文件搜索引擎FileSearch
SDCard 中,文件搜索与File 类
activity_main.xml
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/widget0" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/Button_Search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="253px" android:layout_y="5px" android:text="@string/toSearch" > </Button> <EditText android:id="@+id/input_KEY_EditText" android:layout_width="112px" android:layout_height="52px" android:layout_x="119px" android:layout_y="4px" android:textSize="18sp" > </EditText> <TextView android:id="@+id/TextView_showIn" android:layout_width="103px" android:layout_height="29px" android:layout_x="5px" android:layout_y="16px" android:text="@string/showInput" android:textSize="20sp" > </TextView> <TextView android:id="@+id/TextView_Result" android:layout_width="fill_parent" android:layout_height="370px" android:layout_x="0px" android:layout_y="60px" > </TextView> </AbsoluteLayout>
strings.xml
<resources> <string name="app_name">Demo</string> <string name="hello_world">Hello world!</string> <string name="menu_settings">Settings</string> <string name="title_activity_main">MainActivity</string> <string name="showInput">输入关键字</string> <string name="toSearch">搜索</string> <string name="info">系统SDCard目录文件路径:\n</string> <string name="pleaseInput">请输入关键字!!</string> <string name="notFond">没有找到相关文件!!</string> <string name="pathError">读取路径出错!!</string> </resources>
MainActivity.java
package com.example.demo; import java.io.File; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity implements Button.OnClickListener { /** Called when the activity is first created. */ /* 定义程序要使用的类对象 */ private File file; private String path; private String info; private String theKey_formInput; private TextView show_Result; private EditText input_SearchKey_Edit; private Button toSearch_Button; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /* 通过findViewById()获取XML中的UI对象 */ show_Result = (TextView) findViewById(R.id.TextView_Result); input_SearchKey_Edit = (EditText) findViewById(R.id.input_KEY_EditText); toSearch_Button = (Button) findViewById(R.id.Button_Search); /* 为搜索按钮添加点击事件监听器 */ toSearch_Button.setOnClickListener(this); /* 初始化一个Field 对象,指定路径为/sdcard */ file = new File("/sdcard"); /* 从xml中获取字符串 */ info = getString(R.string.info); } /* * 按 钮 点 击 事 件 处 理 */ public void onClick(View v) { // TODO Auto-generated method stub /* 清空 */ path = ""; show_Result.setText(""); /* 取得输入框中的要查询的Key */ theKey_formInput = input_SearchKey_Edit.getText().toString(); /* 浏览文件 */ BrowserFile(file); } /* * 浏 览 文 件 方 法 */ public void BrowserFile(File file) { if (theKey_formInput.equals("")) { /* 如果输入框没有输入点击搜索按钮,提示输入 */ Toast.makeText(this, getString(R.string.pleaseInput), Toast.LENGTH_SHORT).show(); } else { /* 开始搜索文件 */ ToSearchFiles(file); /* 搜索完毕后,如果搜到结果为空,提示没有找到 */ if (show_Result.getText().equals("")) { Toast.makeText(this, getString(R.string.notFond), Toast.LENGTH_SHORT).show(); } } }/* * 开 始 搜 索 文 件 方 法 */ public void ToSearchFiles(File file) { /* 定义一个File文件数组,用来存放/sdcard目录下的文件或文件夹 */ File[] the_Files = file.listFiles(); /* 通过遍历所有文件和文件夹 */ for (File tempF : the_Files) { if (tempF.isDirectory()) { ToSearchFiles(tempF); /* 如果是文件夹的话继续遍历搜索 */ } else { try { /* 是文件,进行比较,如果文件名称中包含输入搜索Key,则返回大于-1的值 */ if (tempF.getName().indexOf(theKey_formInput) > -1) { /* 获取符合条件文件的路径,进行累加 */ path += "\n" + tempF.getPath(); /* 显示结果的TextView显示信息和搜索到的路径 */ show_Result.setText(info + path); } } catch (Exception e) { // TODO: handle exception /* 如果路径找不到,提示出错 */ Toast.makeText(this, getString(R.string.pathError), Toast.LENGTH_SHORT).show(); } } } } }
----