效果:

1.点击图标进入页面二

2.页面2图片暂停显示5秒进入页面三

3.点击页面三登陆按钮,打开登陆对话框,输入密码进入页面四

点击下载按钮,显示水平进度条

点击保存和获取用户名和密码 进入页面六 操作SharedPreferences

点击文本和文件 进入页面八

4.页面四是一个用BaseAdapt适配器加载的ListView,点击相册行时,跳转至页面五

点击地图时,跳转值页面七,gridview视图(图+字)

5.页面五是一个用GridView建的网格视图(图)

6.页面六 操作SharedPreferences

7.页面八,操作文本和文件的保存和获取

MainActivity.java

  1. package com.example.chenshuai.test404;
  2.  
  3. import android.content.Intent;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7.  
  8. public class MainActivity extends AppCompatActivity {
  9.  
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. }
  15.  
  16. public void onclick(View view)
  17. {
  18. Intent intent = new Intent(this,Activity1.class);
  19. startActivity(intent);
  20. }
  21. }

activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:paddingBottom="@dimen/activity_vertical_margin"
  7. android:paddingLeft="@dimen/activity_horizontal_margin"
  8. android:paddingRight="@dimen/activity_horizontal_margin"
  9. android:paddingTop="@dimen/activity_vertical_margin"
  10. tools:context="com.example.chenshuai.test404.MainActivity">
  11.  
  12. <ImageButton
  13. android:layout_width="90dp"
  14. android:layout_height="90dp"
  15. android:src="@drawable/logo"
  16. android:onClick="onclick"/>
  17. </RelativeLayout>

Activity1.java

  1. package com.example.chenshuai.test404;
  2.  
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.support.v7.app.AppCompatActivity;
  6.  
  7. import java.util.Timer;
  8. import java.util.TimerTask;
  9.  
  10. /**
  11. * Created by chenshuai on 2016/4/3.
  12. */
  13. public class Activity1 extends AppCompatActivity {
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity1_layout);
  18.  
  19. //图片暂停5秒后显示
  20. final Intent it = new Intent(this, Activity2.class); //你要转向的Activity
  21. Timer timer = new Timer();
  22. TimerTask task = new TimerTask() {
  23. @Override
  24. public void run() {
  25. startActivity(it); //执行
  26. }
  27. };
  28. timer.schedule(task, 1000 * 5); //5秒后
  29.  
  30. }
  31. }

activity1_layout.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5.  
  6. <ImageView
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent"
  9. android:background="@drawable/denglu"/>
  10.  
  11. </LinearLayout>

Activity2.java

  1. package com.example.chenshuai.test404;
  2.  
  3. import android.app.AlertDialog;
  4. import android.app.ProgressDialog;
  5. import android.content.DialogInterface;
  6. import android.content.Intent;
  7. import android.os.Bundle;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.view.LayoutInflater;
  10. import android.view.View;
  11. import android.widget.EditText;
  12. import android.widget.Toast;
  13.  
  14. /**
  15. * Created by chenshuai on 2016/4/3.
  16. */
  17. public class Activity2 extends AppCompatActivity {
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity2_layout);
  22.  
  23. }
  24.  
  25. public void onclik1(View view)
  26. {
  27.  
  28. //获取加载器
  29. LayoutInflater layoutInflater = getLayoutInflater();
  30. //加载layout文件
  31. View vi_1 = layoutInflater.inflate(R.layout.login_layout,null);
  32. //添加按钮
  33. new AlertDialog.Builder(this)
  34. .setView(vi_1)
  35. .setNegativeButton("取消", null)
  36. .setPositiveButton("登陆", new DialogInterface.OnClickListener() {
  37. @Override
  38. public void onClick(DialogInterface dialog, int which) {
  39.  
  40. AlertDialog al = (AlertDialog) dialog;
  41.  
  42. EditText pwd = (EditText) al.findViewById(R.id.pwd);
  43.  
  44. String str = pwd.getText().toString();
  45.  
  46. if (str.equals("123")) {
  47. Intent intent = new Intent(Activity2.this, Activity3.class);
  48. startActivity(intent);
  49. } else {
  50. Toast.makeText(Activity2.this, "密码错误!", Toast.LENGTH_SHORT).show();
  51. }
  52.  
  53. }
  54. })
  55. .show();
  56. }
  57. public void onclick2(View v)
  58. {
  59. final ProgressDialog pd = new ProgressDialog(this);
  60. pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  61.  
  62. pd.setMessage("下载进度");
  63. pd.show();
  64.  
  65. //创建thread实例 重写run方法 启动多线程
  66. new Thread()
  67. {
  68. @Override
  69. public void run() {
  70. super.run();
  71. for (int i = 0;i<=pd.getMax();i++)
  72. {
  73. try {
  74. Thread.sleep(100);
  75. }catch (Exception e)
  76. {}
  77. pd.setProgress(i);
  78. }
  79. pd.dismiss();
  80. }
  81. }.start();
  82. }
  83.  
  84. public void onclick3(View view)
  85. {
  86.  
  87. Intent intent = new Intent(this,Activity5.class);
  88. startActivity(intent);
  89. }
  90. public void onclick4(View view)
  91. {
  92. Intent intent = new Intent(this,Activity7.class);
  93. startActivity(intent);
  94. }
  95.  
  96. }

activity2_layout.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6.  
  7. <Button
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:layout_gravity="center_horizontal"
  11. android:background="@drawable/anniu1"
  12. android:text="点击登陆"
  13. android:textSize="20dp"
  14. android:onClick="onclik1"
  15. />
  16. <Button
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:background="@drawable/anniu2"
  20. android:text="点击下载文件"
  21. android:textSize="20dp"
  22. android:onClick="onclick2"
  23. />
  24. <Button
  25. android:layout_width="match_parent"
  26. android:layout_height="wrap_content"
  27. android:text="点击保存用户名和密码 key/value"
  28. android:textSize="20dp"
  29. android:background="@drawable/anniu13"
  30. android:onClick="onclick3"/>
  31. <Button
  32. android:layout_width="match_parent"
  33. android:layout_height="wrap_content"
  34. android:text="点击文本和文件"
  35. android:textSize="20dp"
  36. android:background="@drawable/anniu1"
  37. android:onClick="onclick4"/>
  38.  
  39. </LinearLayout>

login_layout.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6.  
  7. <ImageView
  8. android:layout_width="90dp"
  9. android:layout_height="90dp"
  10. android:src="@drawable/touxiang"
  11. android:layout_marginTop="100dp"
  12. android:layout_gravity="center_horizontal"/>
  13. <TextView
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:text="15275969278"
  17. android:textSize="20dp"
  18. android:layout_gravity="center_horizontal"
  19. android:layout_marginTop="10dp"
  20. />
  21. <EditText
  22. android:layout_width="350dp"
  23. android:layout_height="wrap_content"
  24. android:layout_gravity="center_horizontal"
  25. android:hint="请填写密码"
  26. android:inputType="textPassword"
  27. android:drawableLeft="@drawable/suo1"
  28. android:layout_marginTop="20dp"
  29. android:id="@+id/pwd"
  30. />
  31.  
  32. </LinearLayout>

Activity3.java

  1. package com.example.chenshuai.test404;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.AdapterView;
  10. import android.widget.BaseAdapter;
  11. import android.widget.ImageView;
  12. import android.widget.ListView;
  13. import android.widget.TextView;
  14.  
  15. import java.util.ArrayList;
  16. import java.util.List;
  17.  
  18. /**
  19. * Created by chenshuai on 2016/4/4.
  20. */
  21.  
  22. public class Activity3 extends Activity {
  23. List<Biaozhi> bz;
  24.  
  25. @Override
  26. protected void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.base_layout);
  29.  
  30. ListView base_1 = (ListView)findViewById(R.id.base_1);
  31.  
  32. bz = new ArrayList<Biaozhi>();
  33. bz.add(new Biaozhi(R.drawable.dazhong,"大众点评"));
  34. bz.add(new Biaozhi(R.drawable.gallery,"相册"));
  35. bz.add(new Biaozhi(R.drawable.maps,"地图"));
  36. bz.add(new Biaozhi(R.drawable.mm,"聊天"));
  37. bz.add(new Biaozhi(R.drawable.netease,"网易新闻"));
  38. bz.add(new Biaozhi(R.drawable.qq,"QQ"));
  39. bz.add(new Biaozhi(R.drawable.settings,"设置"));
  40. bz.add(new Biaozhi(R.drawable.sinaweibo,"新浪微博"));
  41. bz.add(new Biaozhi(R.drawable.stormsmart,"暴风影音"));
  42. bz.add(new Biaozhi(R.drawable.taobao,"淘宝"));
  43. bz.add(new Biaozhi(R.drawable.videoplayer,"视频"));
  44.  
  45. base_1.setAdapter(new mybaseadapt());
  46.  
  47. base_1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  48. @Override
  49. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  50.  
  51. if (position == 1)
  52. {
  53. Intent in = new Intent(Activity3.this,Activity4.class);
  54.  
  55. startActivity(in);
  56. }
  57. if (position ==2)
  58. {
  59. Intent in = new Intent(Activity3.this,Activity6.class);
  60.  
  61. startActivity(in);
  62. }
  63. }
  64. });
  65. }
  66.  
  67. //抽象类 要继承并实现抽象方法后才能使用
  68. class mybaseadapt extends BaseAdapter{
  69. @Override
  70. public int getCount() {
  71. return bz.size();
  72. }
  73.  
  74. @Override
  75. public Object getItem(int position) {
  76. return bz.get(position);
  77. }
  78.  
  79. @Override
  80. public long getItemId(int position) {
  81. return position;
  82. }
  83.  
  84. //通过getView来实现Listview
  85. //给视图适配数据返回view视图
  86. //position 数据的下标(第几个视图)
  87. //convertView 可重复使用的视图
  88. //parent 父视图
  89. @Override
  90. public View getView(int position, View convertView, ViewGroup parent) {
  91.  
  92. //如果convertView为null,就加载layout文件
  93. if (convertView == null)
  94. {
  95. //1.获取layout文件 用加载器
  96. LayoutInflater inflater = getLayoutInflater();
  97.  
  98. convertView = inflater.inflate(R.layout.activity3_layout,null);
  99.  
  100. }
  101.  
  102. //2.把数据放入layout文件中 数据和layout文件的视图组件进行绑定
  103. //(1)从数据源里根据position获取数据
  104. Biaozhi biaozhi = bz.get(position);
  105.  
  106. //(2)获取layout文件的视图组件
  107. ImageView iv_1 = (ImageView)convertView.findViewById(R.id.iv_1);
  108. TextView tv_1 = (TextView)convertView.findViewById(R.id.tv_1);
  109.  
  110. //(3)绑定 数据适配
  111. iv_1.setImageResource(biaozhi.getImage());
  112. tv_1.setText(biaozhi.getName());
  113.  
  114. //3.返回视图
  115. return convertView;
  116. }
  117. }
  118. }

base_layout.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <ListView xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:id="@+id/base_1">
  6.  
  7. </ListView>

activity3_layout.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:layout_marginTop="20dp">
  6.  
  7. <ImageView
  8. android:layout_width="80dp"
  9. android:layout_height="80dp"
  10. android:src="@drawable/logo"
  11. android:id="@+id/iv_1"/>
  12.  
  13. <LinearLayout
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:orientation="vertical"
  17. android:layout_gravity="center_vertical"
  18. android:paddingLeft="20dp">
  19.  
  20. <TextView
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:text="微信"
  24. android:textSize="20dp"
  25. android:id="@+id/tv_1"/>
  26.  
  27. </LinearLayout>
  28.  
  29. </LinearLayout>

Activity4.java

  1. package com.example.chenshuai.test404;
  2.  
  3. import android.os.Bundle;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.BaseAdapter;
  8. import android.widget.GridView;
  9. import android.widget.ImageView;
  10.  
  11. public class Activity4 extends AppCompatActivity {
  12.  
  13. int img[] = {R.drawable.dazhong,R.drawable.gallery,R.drawable.maps,R.drawable.mm,
  14. R.drawable.qq,R.drawable.settings,R.drawable.sinaweibo,R.drawable.netease,
  15. R.drawable.stormsmart,R.drawable.torchactivity,R.drawable.videoplayer};
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_4);
  20.  
  21. GridView grv_1 = (GridView)findViewById(R.id.grv_1);
  22.  
  23. grv_1.setAdapter(new mygridview());
  24. }
  25.  
  26. class mygridview extends BaseAdapter{
  27.  
  28. @Override
  29. public int getCount() {
  30. return img.length;
  31. }
  32.  
  33. @Override
  34. public Object getItem(int position) {
  35. return img[position];
  36. }
  37.  
  38. @Override
  39. public long getItemId(int position) {
  40. return position;
  41. }
  42.  
  43. @Override
  44. public View getView(int position, View convertView, ViewGroup parent) {
  45.  
  46. //先取数据
  47. int imageid = img[position];
  48.  
  49. //直接构造视图
  50. ImageView imageView =null;
  51.  
  52. if (convertView == null)
  53. {
  54. imageView = new ImageView(Activity4.this);
  55.  
  56. imageView.setLayoutParams(new GridView.LayoutParams(200,200));
  57. }
  58. else
  59. {
  60. imageView=(ImageView)convertView;
  61. }
  62. //给视图绑定数据
  63. imageView.setImageResource(imageid);
  64.  
  65. return imageView;
  66. }
  67. }
  68. }
  1. activity_4.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <GridView xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:numColumns="4"
  6. android:verticalSpacing="10dp"
  7. android:gravity="center"
  8. android:stretchMode="columnWidth"
  9. android:id="@+id/grv_1"
  10. >
  11.  
  12. </GridView>

Activity5.java

  1. package com.example.chenshuai.test404;
  2.  
  3. import android.content.SharedPreferences;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.EditText;
  8. import android.widget.Toast;
  9.  
  10. public class Activity5 extends AppCompatActivity {
  11.  
  12. EditText user;
  13. EditText pwd;
  14. SharedPreferences sp;
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_5);
  19.  
  20. user = (EditText)findViewById(R.id.user);
  21. pwd = (EditText)findViewById(R.id.pwd);
  22.  
  23. //1.获取sp的实例
  24. sp = getSharedPreferences("mydata",MODE_PRIVATE);
  25. }
  26.  
  27. //保存
  28. public void baocunclick(View view)
  29. {
  30. //1.获取数据
  31. String username = user.getText().toString();
  32. String password = pwd.getText().toString();
  33.  
  34. //判断不为空
  35. if (username.length() == 0 || password.length() ==0)
  36. {
  37.  
  38. Toast.makeText(Activity5.this, "用户名和密码不为空", Toast.LENGTH_SHORT).show();
  39. }
  40. else
  41. {
  42. //2.用Editor存储数据
  43. //获取Editor
  44. SharedPreferences.Editor editor_1 = sp.edit();
  45.  
  46. //3.往编辑器里放数据(从上面获取的数据)
  47. editor_1.putString(username,password);
  48.  
  49. //4.提交保存
  50. boolean b = editor_1.commit();
  51.  
  52. if (b)
  53. {
  54. Toast.makeText(Activity5.this, "保存成功", Toast.LENGTH_SHORT).show();
  55. }
  56. else
  57. {
  58. Toast.makeText(Activity5.this, "保存失败", Toast.LENGTH_SHORT).show();
  59. }
  60.  
  61. }
  62.  
  63. }
  64.  
  65. //获取
  66. public void huoquclick(View view)
  67. {
  68. //1.获取要读的key;
  69. String key = user.getText().toString();
  70.  
  71. //2.将通过用户名读取的密码放入第二个Edittext
  72. pwd.setText(sp.getString(key,"没有发现用户名"));
  73. }
  74. }

activity_5.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context="com.example.chenshuai.test404.Activity5"
  7. android:orientation="vertical">
  8.  
  9. <EditText
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:hint="用户名:"
  13. android:id="@+id/user"/>
  14. <EditText
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content"
  17. android:hint="密码:"
  18. android:id="@+id/pwd"/>
  19. <LinearLayout
  20. android:layout_width="match_parent"
  21. android:layout_height="wrap_content">
  22.  
  23. <Button
  24. android:layout_width="0dp"
  25. android:layout_height="wrap_content"
  26. android:layout_weight="1"
  27. android:text="保存"
  28. android:id="@+id/baocun"
  29. android:onClick="baocunclick"/>
  30. <Button
  31. android:layout_width="0dp"
  32. android:layout_height="wrap_content"
  33. android:layout_weight="1"
  34. android:text="获取"
  35. android:id="@+id/huoqu"
  36. android:onClick="huoquclick"/>
  37. </LinearLayout>
  38.  
  39. </LinearLayout>

Activity6.java

  1. package com.example.chenshuai.test404;
  2.  
  3. import android.os.Bundle;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.BaseAdapter;
  9. import android.widget.GridView;
  10. import android.widget.ImageView;
  11. import android.widget.TextView;
  12.  
  13. import java.util.ArrayList;
  14. import java.util.List;
  15.  
  16. public class Activity6 extends AppCompatActivity {
  17.  
  18. List<Biaozhi> bz;
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_6);
  23.  
  24. GridView gv_1 = (GridView)findViewById(R.id.gv_1);
  25.  
  26. bz = new ArrayList<Biaozhi>();
  27. bz.add(new Biaozhi(R.drawable.dazhong,"大众点评"));
  28. bz.add(new Biaozhi(R.drawable.gallery,"相册"));
  29. bz.add(new Biaozhi(R.drawable.maps,"地图"));
  30. bz.add(new Biaozhi(R.drawable.mm,"聊天"));
  31. bz.add(new Biaozhi(R.drawable.netease,"网易新闻"));
  32. bz.add(new Biaozhi(R.drawable.qq,"QQ"));
  33. bz.add(new Biaozhi(R.drawable.settings,"设置"));
  34. bz.add(new Biaozhi(R.drawable.sinaweibo, "新浪微博"));
  35. bz.add(new Biaozhi(R.drawable.stormsmart, "暴风影音"));
  36. bz.add(new Biaozhi(R.drawable.taobao,"淘宝"));
  37. bz.add(new Biaozhi(R.drawable.videoplayer,"视频"));
  38.  
  39. gv_1.setAdapter(new grid_baseadapter());
  40. }
  41.  
  42. //抽象类 要继承并实现抽象方法后才能使用
  43. class grid_baseadapter extends BaseAdapter
  44. {
  45. @Override
  46. public int getCount() {
  47. return bz.size();
  48. }
  49.  
  50. @Override
  51. public Object getItem(int position) {
  52. return bz.get(position);
  53. }
  54.  
  55. @Override
  56. public long getItemId(int position) {
  57. return position;
  58. }
  59.  
  60. //通过getView来实现Listview
  61. //给视图适配数据返回view视图
  62. //position 数据的下标(第几个视图)
  63. //convertView 可重复使用的视图
  64. //parent 父视图
  65. @Override
  66. public View getView(int position, View convertView, ViewGroup parent) {
  67.  
  68. //如果convertView为null,就加载layout文件
  69. if (convertView == null)
  70. {
  71. //1.获取layout文件 用加载器
  72. LayoutInflater inflater = getLayoutInflater();
  73.  
  74. convertView = inflater.inflate(R.layout.layout_6_1,null);
  75.  
  76. }
  77. //2.把数据放入layout文件中 数据和layout文件的视图组件进行绑定
  78. //(1)从数据源里根据position获取数据
  79. Biaozhi biaozhi = bz.get(position);
  80.  
  81. //(2)获取layout文件的视图组件
  82. ImageView iv_2 = (ImageView)convertView.findViewById(R.id.iv_2);
  83. TextView tv_2 = (TextView)convertView.findViewById(R.id.tv_2);
  84.  
  85. //(3)绑定 数据适配
  86. iv_2.setImageResource(biaozhi.getImage());
  87. tv_2.setText(biaozhi.getName());
  88.  
  89. //3.返回视图
  90. return convertView;
  91. }
  92. }
  93. }

Biaozhi.java

  1. package com.example.chenshuai.test404;
  2.  
  3. /**
  4. * Created by chenshuai on 2016/4/7.
  5. */
  6. public class Biaozhi {
  7.  
  8. private int image;
  9.  
  10. private String name;
  11.  
  12. public int getImage() {
  13. return image;
  14. }
  15.  
  16. public String getName() {
  17. return name;
  18. }
  19.  
  20. public void setImage(int image) {
  21. this.image = image;
  22. }
  23.  
  24. public void setName(String name) {
  25. this.name = name;
  26. }
  27.  
  28. public Biaozhi(int image, String name) {
  29. this.image = image;
  30. this.name = name;
  31. }
  32.  
  33. public Biaozhi()
  34. {
  35.  
  36. }
  37.  
  38. @Override
  39. public String toString() {
  40. return "Biaozhi{" +
  41. "image=" + image +
  42. ", name='" + name + '\'' +
  43. '}';
  44. }
  45. }

Activity_6.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <GridView xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:id="@+id/gv_1"
  6. android:numColumns="4"
  7. android:verticalSpacing="10dp"
  8. android:stretchMode="columnWidth"
  9. >
  10. </GridView>

Activity_6_1.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6.  
  7. <ImageView
  8. android:layout_width="120dp"
  9. android:layout_height="120dp"
  10. android:src="@drawable/dazhong"
  11. android:id="@+id/iv_2"/>
  12. <TextView
  13. android:layout_width="120dp"
  14. android:layout_height="wrap_content"
  15. android:text="大众点评"
  16. android:textSize="20dp"
  17. android:gravity="center"
  18. android:id="@+id/tv_2"/>
  19.  
  20. </LinearLayout>

Activity7.java

  1. package com.example.chenshuai.test404;
  2.  
  3. import android.content.res.AssetManager;
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import android.os.Bundle;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.view.View;
  9. import android.widget.EditText;
  10. import android.widget.ImageView;
  11. import android.widget.Toast;
  12.  
  13. import java.io.FileInputStream;
  14. import java.io.FileOutputStream;
  15. import java.io.InputStream;
  16. import java.io.PrintStream;
  17.  
  18. public class Activity7 extends AppCompatActivity {
  19. EditText et_1;
  20. EditText et_2;
  21.  
  22. ImageView iv_3;
  23. @Override
  24. protected void onCreate(Bundle savedInstanceState) {
  25. super.onCreate(savedInstanceState);
  26. setContentView(R.layout.activity_7);
  27.  
  28. et_1 = (EditText)findViewById(R.id.et_1);
  29. et_2 = (EditText)findViewById(R.id.et_2);
  30.  
  31. iv_3 = (ImageView)findViewById(R.id.iv_3);
  32. }
  33.  
  34. final String FILENAME = "text.txt";
  35. public void baocunwenben_onclick(View view)
  36. {
  37.  
  38. //1.获取要存储的内容
  39. String content = et_1.getText().toString();
  40.  
  41. //2.获取输出流 以数据为基准 从手机存储往文件走为输出流
  42. try {
  43. //追加模式
  44. FileOutputStream fos = openFileOutput(FILENAME,MODE_APPEND);
  45.  
  46. //3.构造打印流 PrintStream
  47. PrintStream ps = new PrintStream(fos);
  48.  
  49. //4.写入内容(换行)
  50. ps.println(content);
  51.  
  52. //5.关闭
  53. ps.close();
  54. fos.close();
  55.  
  56. Toast.makeText(Activity7.this, "保存成功", Toast.LENGTH_SHORT).show();
  57.  
  58. } catch (Exception e) {
  59. e.printStackTrace();
  60. Toast.makeText(Activity7.this, "保存失败", Toast.LENGTH_SHORT).show();
  61. }
  62.  
  63. }
  64. //获取
  65. public void huoquwenben_onclick(View view)
  66. {
  67.  
  68. //1.获取输入流 从文件到手机存储
  69. try {
  70. FileInputStream fis = openFileInput(FILENAME);
  71.  
  72. //2.用数组方法读取
  73. //定义读取的数组
  74.  
  75. byte[] b = new byte[1024];
  76. //3.读出的数据的长度
  77. int i = 0;
  78.  
  79. StringBuilder sbr = new StringBuilder();
  80.  
  81. //fis.read(b)返回长度
  82. while ((i = fis.read(b))>0)
  83. {
  84. //在这里需要字符串,转为字符串
  85. sbr.append(new String(b,0,i));
  86. }
  87.  
  88. fis.close();
  89.  
  90. et_2.setText(sbr);
  91.  
  92. Toast.makeText(Activity7.this, "读取成功", Toast.LENGTH_SHORT).show();
  93.  
  94. } catch (Exception e) {
  95. e.printStackTrace();
  96. Toast.makeText(Activity7.this, "读取失败", Toast.LENGTH_SHORT).show();
  97. }
  98. }
  99. //操作assets内的文件
  100. public void baocuntupianwj_onclick(View view)
  101. {
  102. //1.获取AssetManager
  103. AssetManager assetManager = getAssets();
  104.  
  105. //2.打开文件 返回输入流 把文件读到内存里
  106. try {
  107. InputStream is = assetManager.open("zixingche.jpg");
  108.  
  109. //3.获取输出流
  110. FileOutputStream fos = openFileOutput("zixingche2.jpg",MODE_PRIVATE);
  111.  
  112. //4.边读边写
  113. byte[] b =new byte[1024];
  114.  
  115. int i = 0;
  116.  
  117. while((i = is.read(b))>0)
  118. {
  119. fos.write(b, 0, i);
  120. }
  121.  
  122. fos.close();
  123. is.close();
  124.  
  125. Toast.makeText(Activity7.this, "保存成功", Toast.LENGTH_SHORT).show();
  126.  
  127. } catch (Exception e) {
  128. e.printStackTrace();
  129. }
  130. }
  131. //读取文件 从手机内部存储读图片文件
  132. public void huoqutupianwj_onclick(View view)
  133. {
  134. //改变ImageView的图片来源,指向手机存储空间
  135.  
  136. //1.获取文件存储的绝对路径
  137. String filepath = getFilesDir().getAbsolutePath();
  138.  
  139. //2.组合完整路径
  140. filepath += "/zixingche2.jpg";
  141.  
  142. Toast.makeText(Activity7.this, "path= "+filepath, Toast.LENGTH_SHORT).show();
  143.  
  144. //3.生成位图实例
  145. Bitmap bm = BitmapFactory.decodeFile(filepath);
  146.  
  147. //4.改变ImageView的图片来源 显示图片
  148. iv_3.setImageBitmap(bm);
  149. }
  150. }

Activity_7.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. >
  7. <EditText
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:hint="输入要保存的文本"
  11. android:id="@+id/et_1"/>
  12. <EditText
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:hint="显示保存的文本"
  16. android:id="@+id/et_2"/>
  17. <LinearLayout
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. android:orientation="horizontal">
  21.  
  22. <Button
  23. android:layout_width="0dp"
  24. android:layout_height="match_parent"
  25. android:text="点击保存文本"
  26. android:layout_weight="1"
  27. android:onClick="baocunwenben_onclick"/>
  28. <Button
  29. android:layout_width="0dp"
  30. android:layout_height="match_parent"
  31. android:text="点击获取文本"
  32. android:layout_weight="1"
  33. android:onClick="huoquwenben_onclick"/>
  34.  
  35. </LinearLayout>
  36. <LinearLayout
  37. android:layout_width="match_parent"
  38. android:layout_height="wrap_content"
  39. android:orientation="horizontal">
  40.  
  41. <Button
  42. android:layout_width="0dp"
  43. android:layout_height="match_parent"
  44. android:text="点击保存图片文件"
  45. android:layout_weight="1"
  46. android:onClick="baocuntupianwj_onclick"/>
  47. <Button
  48. android:layout_width="0dp"
  49. android:layout_height="match_parent"
  50. android:text="点击获取图片文件"
  51. android:layout_weight="1"
  52. android:onClick="huoqutupianwj_onclick"/>
  53.  
  54. </LinearLayout>
  55. <ImageView
  56. android:layout_width="wrap_content"
  57. android:layout_height="wrap_content"
  58. android:src="@drawable/touxiang"
  59. android:layout_gravity="center"
  60. android:id="@+id/iv_3"
  61. />
  62.  
  63. </LinearLayout>

AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.example.chenshuai.test404">
  4.  
  5. <application
  6. android:allowBackup="true"
  7. android:icon="@mipmap/ic_launcher"
  8. android:label="@string/app_name"
  9. android:supportsRtl="true"
  10. android:theme="@style/AppTheme">
  11. <activity android:name=".MainActivity">
  12. <intent-filter>
  13. <action android:name="android.intent.action.MAIN" />
  14.  
  15. <category android:name="android.intent.category.LAUNCHER" />
  16. </intent-filter>
  17. </activity>
  18. <activity android:name=".Activity1">
  19. <intent-filter>
  20. <action android:name="android.intent.action.VIEW" />
  21.  
  22. <category android:name="android.intent.category.DEFAULT" />
  23. </intent-filter>
  24. </activity>
  25. <activity android:name=".Activity2">
  26. <intent-filter>
  27. <action android:name="android.intent.action.VIEW" />
  28.  
  29. <category android:name="android.intent.category.DEFAULT" />
  30. </intent-filter>
  31. </activity>
  32. <activity android:name=".Activity3">
  33. <intent-filter>
  34. <action android:name="android.intent.action.VIEW" />
  35.  
  36. <category android:name="android.intent.category.DEFAULT" />
  37. </intent-filter>
  38. </activity>
  39. <activity android:name=".Activity4">
  40. <intent-filter>
  41. <action android:name="android.intent.action.VIEW" />
  42.  
  43. <category android:name="android.intent.category.DEFAULT" />
  44. </intent-filter>
  45. </activity>
  46. <activity android:name=".Activity5">
  47. <intent-filter>
  48. <action android:name="android.intent.action.VIEW" />
  49.  
  50. <category android:name="android.intent.category.DEFAULT" />
  51. </intent-filter>
  52. </activity>
  53. <activity android:name=".Activity6">
  54. <intent-filter>
  55. <action android:name="android.intent.action.VIEW" />
  56.  
  57. <category android:name="android.intent.category.DEFAULT" />
  58. </intent-filter>
  59. </activity>
  60. <activity android:name=".Activity7">
  61. <intent-filter>
  62. <action android:name="android.intent.action.VIEW" />
  63.  
  64. <category android:name="android.intent.category.DEFAULT" />
  65. </intent-filter>
  66. </activity>
  67. </application>
  68.  
  69. </manifest>

AndroidManifest.xml

Android——用对话框做登陆界面(自定义对话框AlertDialog,多线程,进度条ProgressDialog,ListView,GridView,SharedPreferences存,读数据,存取文本,assets文件)的更多相关文章

  1. Android 自定义漂亮的圆形进度条

    公司有这样一个需求,实现这个圆弧进度条 所以,现在就将它抽取出来分享 如果需要是圆帽的就将,下面这句代码放开即可 mRingPaint.setStrokeCap(Paint.Cap.ROUND);// ...

  2. android多线程进度条

    多线程实现更新android进度条. 实例教程,详细信息我已经注释   android多线程进度条   01package com.shougao.hello; 02 03import android ...

  3. Android中ProgressBar的使用-通过Handler与Message实现进度条显示

    场景 进度条效果 注: 博客: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实现 将布局改为 ...

  4. android——利用SharedPreference做引导界面

    很久以前就接触过sharedPreference这个android中的存储介质.但是一直没有实际使用过,今天在看之前做的“民用机型大全”的app时,突然想到可以使用sharedPreference类来 ...

  5. android 对话框中的进度条 (ProgressDialog)

    from:http://byandby.iteye.com/blog/817214 显然要定义对话框进度条就要用ProgressDialog,首先我们需要创建ProgressDialog对象,当然这里 ...

  6. MFC3 基本对话框的使用(三) 滑块与进度条(sdnu)(C++大作业)

    一.完成界面 运行前: 运行后: 二.工具 (1)滑块 (2)进度条 (3)文本框 (4)文本示例 (5)按钮 三.添加变量 四.添加事件 右键单击主对话框空白部分,打开类向导,选择"消息& ...

  7. Android自定义一款带进度条的精美按键

    Android中自定义View并没有什么可怕的,拿到一个需要自定义的View,首先要做的就是把它肢解,然后思考每一步是怎样实现的,按分析的步骤一步一步的编码实现,最后你就会发现达到了你想要的效果.本文 ...

  8. [置顶] 自定义的解压进度条 关于ProgressBar的使用

    整体布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android ...

  9. Android BGradualProgress 多种渐变、直角or弧角、进度条、加载条

    可实现多种渐变.直角or弧角.进度条.加载条 (Various gradient, right or arc angle, progress bar and loading bar can be re ...

随机推荐

  1. Linux-Memcache和Redis常用命令

    Memcache:    支持类型: String     add, delete, set, replace, get, flush_all, stats, stats reset, stats i ...

  2. ACM遇到的问题与解决方案

    C++防止栈溢出措施: 只要在你的代码里加上下面这句话, OK,栈溢出直接搞定!!! #pragma comment(linker, "/STACK:102400000,102400000& ...

  3. dokuwiki语法

    dokuwiki是一个php写的维基系统,它的插件中包含markdown插件.但是markdown语法跟dokuwiki语法混着用会出现一些bug.所以还是学一下dokuwiki的语法吧. dokuw ...

  4. java.sql.SQLException: 索引中丢失 IN 或 OUT 参数:: 1

    String sql1 = "insert into TEST_RELEVANCEEXPORT" +            " (ID, YYCSDM, YYCSMC, ...

  5. 在Android 开发中使用 SQLite 数据库笔记

    SQLite 介绍   SQLite 一个非常流行的嵌入式数据库,它支持 SQL 语言,并且只利用很少的内存就有很好的性能.此外它还是开源的,任何人都可以使用它.许多开源项目((Mozilla, PH ...

  6. 设置Adobe Reader打开PDF文件保持记忆功能

    设置Adobe Reader打开PDF文件保持记忆功能 打开菜单“编辑”->“首选项”. 选择种类中的“文档”,在“打开设置”区域勾上“重新打开文档时恢复上次视图设置(R)”,确定之后就可以在下 ...

  7. AP_自动付款工作台设定和操作(流程)

    2014-06-04 Created By BaoXinjian

  8. Python2 字典 has_key() 方法

    描述 Python2 字典 has_key() 方法用于判断键(key)是否存在于字典(D)中,如果键在字典中返回True,否则返回False. 官方文档推荐用 in 操作符,因为它更短更通俗易懂.h ...

  9. debian系在线安装软件apt-get命令族

    一.背景 apt-get install/remove在线安装/卸载文件真是方便极了. 但是有时候安装/卸载文件不清楚文件在服务器上的实际命名,例如想安装sndfile.应该执行下面哪个命令呢? ap ...

  10. 谱聚类(Spectral clustering)(1):RatioCut

    作者:桂. 时间:2017-04-13  19:14:48 链接:http://www.cnblogs.com/xingshansi/p/6702174.html 声明:本文大部分内容来自:刘建平Pi ...