Android——用对话框做登陆界面(自定义对话框AlertDialog,多线程,进度条ProgressDialog,ListView,GridView,SharedPreferences存,读数据,存取文本,assets文件)
效果:
1.点击图标进入页面二
2.页面2图片暂停显示5秒进入页面三
3.点击页面三登陆按钮,打开登陆对话框,输入密码进入页面四
点击下载按钮,显示水平进度条
点击保存和获取用户名和密码 进入页面六 操作SharedPreferences
点击文本和文件 进入页面八
4.页面四是一个用BaseAdapt适配器加载的ListView,点击相册行时,跳转至页面五
点击地图时,跳转值页面七,gridview视图(图+字)
5.页面五是一个用GridView建的网格视图(图)
6.页面六 操作SharedPreferences
7.页面八,操作文本和文件的保存和获取
MainActivity.java
- package com.example.chenshuai.test404;
- import android.content.Intent;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- public class MainActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- public void onclick(View view)
- {
- Intent intent = new Intent(this,Activity1.class);
- startActivity(intent);
- }
- }
activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context="com.example.chenshuai.test404.MainActivity">
- <ImageButton
- android:layout_width="90dp"
- android:layout_height="90dp"
- android:src="@drawable/logo"
- android:onClick="onclick"/>
- </RelativeLayout>
Activity1.java
- package com.example.chenshuai.test404;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import java.util.Timer;
- import java.util.TimerTask;
- /**
- * Created by chenshuai on 2016/4/3.
- */
- public class Activity1 extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity1_layout);
- //图片暂停5秒后显示
- final Intent it = new Intent(this, Activity2.class); //你要转向的Activity
- Timer timer = new Timer();
- TimerTask task = new TimerTask() {
- @Override
- public void run() {
- startActivity(it); //执行
- }
- };
- timer.schedule(task, 1000 * 5); //5秒后
- }
- }
activity1_layout.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <ImageView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@drawable/denglu"/>
- </LinearLayout>
Activity2.java
- package com.example.chenshuai.test404;
- import android.app.AlertDialog;
- import android.app.ProgressDialog;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
- /**
- * Created by chenshuai on 2016/4/3.
- */
- public class Activity2 extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity2_layout);
- }
- public void onclik1(View view)
- {
- //获取加载器
- LayoutInflater layoutInflater = getLayoutInflater();
- //加载layout文件
- View vi_1 = layoutInflater.inflate(R.layout.login_layout,null);
- //添加按钮
- new AlertDialog.Builder(this)
- .setView(vi_1)
- .setNegativeButton("取消", null)
- .setPositiveButton("登陆", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- AlertDialog al = (AlertDialog) dialog;
- EditText pwd = (EditText) al.findViewById(R.id.pwd);
- String str = pwd.getText().toString();
- if (str.equals("123")) {
- Intent intent = new Intent(Activity2.this, Activity3.class);
- startActivity(intent);
- } else {
- Toast.makeText(Activity2.this, "密码错误!", Toast.LENGTH_SHORT).show();
- }
- }
- })
- .show();
- }
- public void onclick2(View v)
- {
- final ProgressDialog pd = new ProgressDialog(this);
- pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
- pd.setMessage("下载进度");
- pd.show();
- //创建thread实例 重写run方法 启动多线程
- new Thread()
- {
- @Override
- public void run() {
- super.run();
- for (int i = 0;i<=pd.getMax();i++)
- {
- try {
- Thread.sleep(100);
- }catch (Exception e)
- {}
- pd.setProgress(i);
- }
- pd.dismiss();
- }
- }.start();
- }
- public void onclick3(View view)
- {
- Intent intent = new Intent(this,Activity5.class);
- startActivity(intent);
- }
- public void onclick4(View view)
- {
- Intent intent = new Intent(this,Activity7.class);
- startActivity(intent);
- }
- }
activity2_layout.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:background="@drawable/anniu1"
- android:text="点击登陆"
- android:textSize="20dp"
- android:onClick="onclik1"
- />
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@drawable/anniu2"
- android:text="点击下载文件"
- android:textSize="20dp"
- android:onClick="onclick2"
- />
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="点击保存用户名和密码 key/value"
- android:textSize="20dp"
- android:background="@drawable/anniu13"
- android:onClick="onclick3"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="点击文本和文件"
- android:textSize="20dp"
- android:background="@drawable/anniu1"
- android:onClick="onclick4"/>
- </LinearLayout>
login_layout.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <ImageView
- android:layout_width="90dp"
- android:layout_height="90dp"
- android:src="@drawable/touxiang"
- android:layout_marginTop="100dp"
- android:layout_gravity="center_horizontal"/>
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="15275969278"
- android:textSize="20dp"
- android:layout_gravity="center_horizontal"
- android:layout_marginTop="10dp"
- />
- <EditText
- android:layout_width="350dp"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:hint="请填写密码"
- android:inputType="textPassword"
- android:drawableLeft="@drawable/suo1"
- android:layout_marginTop="20dp"
- android:id="@+id/pwd"
- />
- </LinearLayout>
Activity3.java
- package com.example.chenshuai.test404;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AdapterView;
- import android.widget.BaseAdapter;
- import android.widget.ImageView;
- import android.widget.ListView;
- import android.widget.TextView;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * Created by chenshuai on 2016/4/4.
- */
- public class Activity3 extends Activity {
- List<Biaozhi> bz;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.base_layout);
- ListView base_1 = (ListView)findViewById(R.id.base_1);
- bz = new ArrayList<Biaozhi>();
- bz.add(new Biaozhi(R.drawable.dazhong,"大众点评"));
- bz.add(new Biaozhi(R.drawable.gallery,"相册"));
- bz.add(new Biaozhi(R.drawable.maps,"地图"));
- bz.add(new Biaozhi(R.drawable.mm,"聊天"));
- bz.add(new Biaozhi(R.drawable.netease,"网易新闻"));
- bz.add(new Biaozhi(R.drawable.qq,"QQ"));
- bz.add(new Biaozhi(R.drawable.settings,"设置"));
- bz.add(new Biaozhi(R.drawable.sinaweibo,"新浪微博"));
- bz.add(new Biaozhi(R.drawable.stormsmart,"暴风影音"));
- bz.add(new Biaozhi(R.drawable.taobao,"淘宝"));
- bz.add(new Biaozhi(R.drawable.videoplayer,"视频"));
- base_1.setAdapter(new mybaseadapt());
- base_1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- if (position == 1)
- {
- Intent in = new Intent(Activity3.this,Activity4.class);
- startActivity(in);
- }
- if (position ==2)
- {
- Intent in = new Intent(Activity3.this,Activity6.class);
- startActivity(in);
- }
- }
- });
- }
- //抽象类 要继承并实现抽象方法后才能使用
- class mybaseadapt extends BaseAdapter{
- @Override
- public int getCount() {
- return bz.size();
- }
- @Override
- public Object getItem(int position) {
- return bz.get(position);
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- //通过getView来实现Listview
- //给视图适配数据返回view视图
- //position 数据的下标(第几个视图)
- //convertView 可重复使用的视图
- //parent 父视图
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- //如果convertView为null,就加载layout文件
- if (convertView == null)
- {
- //1.获取layout文件 用加载器
- LayoutInflater inflater = getLayoutInflater();
- convertView = inflater.inflate(R.layout.activity3_layout,null);
- }
- //2.把数据放入layout文件中 数据和layout文件的视图组件进行绑定
- //(1)从数据源里根据position获取数据
- Biaozhi biaozhi = bz.get(position);
- //(2)获取layout文件的视图组件
- ImageView iv_1 = (ImageView)convertView.findViewById(R.id.iv_1);
- TextView tv_1 = (TextView)convertView.findViewById(R.id.tv_1);
- //(3)绑定 数据适配
- iv_1.setImageResource(biaozhi.getImage());
- tv_1.setText(biaozhi.getName());
- //3.返回视图
- return convertView;
- }
- }
- }
base_layout.xml
- <?xml version="1.0" encoding="utf-8"?>
- <ListView xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:id="@+id/base_1">
- </ListView>
activity3_layout.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="20dp">
- <ImageView
- android:layout_width="80dp"
- android:layout_height="80dp"
- android:src="@drawable/logo"
- android:id="@+id/iv_1"/>
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="vertical"
- android:layout_gravity="center_vertical"
- android:paddingLeft="20dp">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="微信"
- android:textSize="20dp"
- android:id="@+id/tv_1"/>
- </LinearLayout>
- </LinearLayout>
Activity4.java
- package com.example.chenshuai.test404;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.GridView;
- import android.widget.ImageView;
- public class Activity4 extends AppCompatActivity {
- int img[] = {R.drawable.dazhong,R.drawable.gallery,R.drawable.maps,R.drawable.mm,
- R.drawable.qq,R.drawable.settings,R.drawable.sinaweibo,R.drawable.netease,
- R.drawable.stormsmart,R.drawable.torchactivity,R.drawable.videoplayer};
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_4);
- GridView grv_1 = (GridView)findViewById(R.id.grv_1);
- grv_1.setAdapter(new mygridview());
- }
- class mygridview extends BaseAdapter{
- @Override
- public int getCount() {
- return img.length;
- }
- @Override
- public Object getItem(int position) {
- return img[position];
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- //先取数据
- int imageid = img[position];
- //直接构造视图
- ImageView imageView =null;
- if (convertView == null)
- {
- imageView = new ImageView(Activity4.this);
- imageView.setLayoutParams(new GridView.LayoutParams(200,200));
- }
- else
- {
- imageView=(ImageView)convertView;
- }
- //给视图绑定数据
- imageView.setImageResource(imageid);
- return imageView;
- }
- }
- }
- activity_4.xml
- <?xml version="1.0" encoding="utf-8"?>
- <GridView xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:numColumns="4"
- android:verticalSpacing="10dp"
- android:gravity="center"
- android:stretchMode="columnWidth"
- android:id="@+id/grv_1"
- >
- </GridView>
Activity5.java
- package com.example.chenshuai.test404;
- import android.content.SharedPreferences;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
- public class Activity5 extends AppCompatActivity {
- EditText user;
- EditText pwd;
- SharedPreferences sp;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_5);
- user = (EditText)findViewById(R.id.user);
- pwd = (EditText)findViewById(R.id.pwd);
- //1.获取sp的实例
- sp = getSharedPreferences("mydata",MODE_PRIVATE);
- }
- //保存
- public void baocunclick(View view)
- {
- //1.获取数据
- String username = user.getText().toString();
- String password = pwd.getText().toString();
- //判断不为空
- if (username.length() == 0 || password.length() ==0)
- {
- Toast.makeText(Activity5.this, "用户名和密码不为空", Toast.LENGTH_SHORT).show();
- }
- else
- {
- //2.用Editor存储数据
- //获取Editor
- SharedPreferences.Editor editor_1 = sp.edit();
- //3.往编辑器里放数据(从上面获取的数据)
- editor_1.putString(username,password);
- //4.提交保存
- boolean b = editor_1.commit();
- if (b)
- {
- Toast.makeText(Activity5.this, "保存成功", Toast.LENGTH_SHORT).show();
- }
- else
- {
- Toast.makeText(Activity5.this, "保存失败", Toast.LENGTH_SHORT).show();
- }
- }
- }
- //获取
- public void huoquclick(View view)
- {
- //1.获取要读的key;
- String key = user.getText().toString();
- //2.将通过用户名读取的密码放入第二个Edittext
- pwd.setText(sp.getString(key,"没有发现用户名"));
- }
- }
activity_5.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="com.example.chenshuai.test404.Activity5"
- android:orientation="vertical">
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="用户名:"
- android:id="@+id/user"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="密码:"
- android:id="@+id/pwd"/>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <Button
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="保存"
- android:id="@+id/baocun"
- android:onClick="baocunclick"/>
- <Button
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="获取"
- android:id="@+id/huoqu"
- android:onClick="huoquclick"/>
- </LinearLayout>
- </LinearLayout>
Activity6.java
- package com.example.chenshuai.test404;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.GridView;
- import android.widget.ImageView;
- import android.widget.TextView;
- import java.util.ArrayList;
- import java.util.List;
- public class Activity6 extends AppCompatActivity {
- List<Biaozhi> bz;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_6);
- GridView gv_1 = (GridView)findViewById(R.id.gv_1);
- bz = new ArrayList<Biaozhi>();
- bz.add(new Biaozhi(R.drawable.dazhong,"大众点评"));
- bz.add(new Biaozhi(R.drawable.gallery,"相册"));
- bz.add(new Biaozhi(R.drawable.maps,"地图"));
- bz.add(new Biaozhi(R.drawable.mm,"聊天"));
- bz.add(new Biaozhi(R.drawable.netease,"网易新闻"));
- bz.add(new Biaozhi(R.drawable.qq,"QQ"));
- bz.add(new Biaozhi(R.drawable.settings,"设置"));
- bz.add(new Biaozhi(R.drawable.sinaweibo, "新浪微博"));
- bz.add(new Biaozhi(R.drawable.stormsmart, "暴风影音"));
- bz.add(new Biaozhi(R.drawable.taobao,"淘宝"));
- bz.add(new Biaozhi(R.drawable.videoplayer,"视频"));
- gv_1.setAdapter(new grid_baseadapter());
- }
- //抽象类 要继承并实现抽象方法后才能使用
- class grid_baseadapter extends BaseAdapter
- {
- @Override
- public int getCount() {
- return bz.size();
- }
- @Override
- public Object getItem(int position) {
- return bz.get(position);
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- //通过getView来实现Listview
- //给视图适配数据返回view视图
- //position 数据的下标(第几个视图)
- //convertView 可重复使用的视图
- //parent 父视图
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- //如果convertView为null,就加载layout文件
- if (convertView == null)
- {
- //1.获取layout文件 用加载器
- LayoutInflater inflater = getLayoutInflater();
- convertView = inflater.inflate(R.layout.layout_6_1,null);
- }
- //2.把数据放入layout文件中 数据和layout文件的视图组件进行绑定
- //(1)从数据源里根据position获取数据
- Biaozhi biaozhi = bz.get(position);
- //(2)获取layout文件的视图组件
- ImageView iv_2 = (ImageView)convertView.findViewById(R.id.iv_2);
- TextView tv_2 = (TextView)convertView.findViewById(R.id.tv_2);
- //(3)绑定 数据适配
- iv_2.setImageResource(biaozhi.getImage());
- tv_2.setText(biaozhi.getName());
- //3.返回视图
- return convertView;
- }
- }
- }
Biaozhi.java
- package com.example.chenshuai.test404;
- /**
- * Created by chenshuai on 2016/4/7.
- */
- public class Biaozhi {
- private int image;
- private String name;
- public int getImage() {
- return image;
- }
- public String getName() {
- return name;
- }
- public void setImage(int image) {
- this.image = image;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Biaozhi(int image, String name) {
- this.image = image;
- this.name = name;
- }
- public Biaozhi()
- {
- }
- @Override
- public String toString() {
- return "Biaozhi{" +
- "image=" + image +
- ", name='" + name + '\'' +
- '}';
- }
- }
Activity_6.xml
- <?xml version="1.0" encoding="utf-8"?>
- <GridView xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:id="@+id/gv_1"
- android:numColumns="4"
- android:verticalSpacing="10dp"
- android:stretchMode="columnWidth"
- >
- </GridView>
Activity_6_1.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <ImageView
- android:layout_width="120dp"
- android:layout_height="120dp"
- android:src="@drawable/dazhong"
- android:id="@+id/iv_2"/>
- <TextView
- android:layout_width="120dp"
- android:layout_height="wrap_content"
- android:text="大众点评"
- android:textSize="20dp"
- android:gravity="center"
- android:id="@+id/tv_2"/>
- </LinearLayout>
Activity7.java
- package com.example.chenshuai.test404;
- import android.content.res.AssetManager;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.widget.Toast;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.PrintStream;
- public class Activity7 extends AppCompatActivity {
- EditText et_1;
- EditText et_2;
- ImageView iv_3;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_7);
- et_1 = (EditText)findViewById(R.id.et_1);
- et_2 = (EditText)findViewById(R.id.et_2);
- iv_3 = (ImageView)findViewById(R.id.iv_3);
- }
- final String FILENAME = "text.txt";
- public void baocunwenben_onclick(View view)
- {
- //1.获取要存储的内容
- String content = et_1.getText().toString();
- //2.获取输出流 以数据为基准 从手机存储往文件走为输出流
- try {
- //追加模式
- FileOutputStream fos = openFileOutput(FILENAME,MODE_APPEND);
- //3.构造打印流 PrintStream
- PrintStream ps = new PrintStream(fos);
- //4.写入内容(换行)
- ps.println(content);
- //5.关闭
- ps.close();
- fos.close();
- Toast.makeText(Activity7.this, "保存成功", Toast.LENGTH_SHORT).show();
- } catch (Exception e) {
- e.printStackTrace();
- Toast.makeText(Activity7.this, "保存失败", Toast.LENGTH_SHORT).show();
- }
- }
- //获取
- public void huoquwenben_onclick(View view)
- {
- //1.获取输入流 从文件到手机存储
- try {
- FileInputStream fis = openFileInput(FILENAME);
- //2.用数组方法读取
- //定义读取的数组
- byte[] b = new byte[1024];
- //3.读出的数据的长度
- int i = 0;
- StringBuilder sbr = new StringBuilder();
- //fis.read(b)返回长度
- while ((i = fis.read(b))>0)
- {
- //在这里需要字符串,转为字符串
- sbr.append(new String(b,0,i));
- }
- fis.close();
- et_2.setText(sbr);
- Toast.makeText(Activity7.this, "读取成功", Toast.LENGTH_SHORT).show();
- } catch (Exception e) {
- e.printStackTrace();
- Toast.makeText(Activity7.this, "读取失败", Toast.LENGTH_SHORT).show();
- }
- }
- //操作assets内的文件
- public void baocuntupianwj_onclick(View view)
- {
- //1.获取AssetManager
- AssetManager assetManager = getAssets();
- //2.打开文件 返回输入流 把文件读到内存里
- try {
- InputStream is = assetManager.open("zixingche.jpg");
- //3.获取输出流
- FileOutputStream fos = openFileOutput("zixingche2.jpg",MODE_PRIVATE);
- //4.边读边写
- byte[] b =new byte[1024];
- int i = 0;
- while((i = is.read(b))>0)
- {
- fos.write(b, 0, i);
- }
- fos.close();
- is.close();
- Toast.makeText(Activity7.this, "保存成功", Toast.LENGTH_SHORT).show();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- //读取文件 从手机内部存储读图片文件
- public void huoqutupianwj_onclick(View view)
- {
- //改变ImageView的图片来源,指向手机存储空间
- //1.获取文件存储的绝对路径
- String filepath = getFilesDir().getAbsolutePath();
- //2.组合完整路径
- filepath += "/zixingche2.jpg";
- Toast.makeText(Activity7.this, "path= "+filepath, Toast.LENGTH_SHORT).show();
- //3.生成位图实例
- Bitmap bm = BitmapFactory.decodeFile(filepath);
- //4.改变ImageView的图片来源 显示图片
- iv_3.setImageBitmap(bm);
- }
- }
Activity_7.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- >
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="输入要保存的文本"
- android:id="@+id/et_1"/>
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="显示保存的文本"
- android:id="@+id/et_2"/>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
- <Button
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:text="点击保存文本"
- android:layout_weight="1"
- android:onClick="baocunwenben_onclick"/>
- <Button
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:text="点击获取文本"
- android:layout_weight="1"
- android:onClick="huoquwenben_onclick"/>
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
- <Button
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:text="点击保存图片文件"
- android:layout_weight="1"
- android:onClick="baocuntupianwj_onclick"/>
- <Button
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:text="点击获取图片文件"
- android:layout_weight="1"
- android:onClick="huoqutupianwj_onclick"/>
- </LinearLayout>
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/touxiang"
- android:layout_gravity="center"
- android:id="@+id/iv_3"
- />
- </LinearLayout>
AndroidManifest.xml
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.chenshuai.test404">
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <activity android:name=".MainActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity android:name=".Activity1">
- <intent-filter>
- <action android:name="android.intent.action.VIEW" />
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- </activity>
- <activity android:name=".Activity2">
- <intent-filter>
- <action android:name="android.intent.action.VIEW" />
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- </activity>
- <activity android:name=".Activity3">
- <intent-filter>
- <action android:name="android.intent.action.VIEW" />
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- </activity>
- <activity android:name=".Activity4">
- <intent-filter>
- <action android:name="android.intent.action.VIEW" />
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- </activity>
- <activity android:name=".Activity5">
- <intent-filter>
- <action android:name="android.intent.action.VIEW" />
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- </activity>
- <activity android:name=".Activity6">
- <intent-filter>
- <action android:name="android.intent.action.VIEW" />
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- </activity>
- <activity android:name=".Activity7">
- <intent-filter>
- <action android:name="android.intent.action.VIEW" />
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
AndroidManifest.xml
Android——用对话框做登陆界面(自定义对话框AlertDialog,多线程,进度条ProgressDialog,ListView,GridView,SharedPreferences存,读数据,存取文本,assets文件)的更多相关文章
- Android 自定义漂亮的圆形进度条
公司有这样一个需求,实现这个圆弧进度条 所以,现在就将它抽取出来分享 如果需要是圆帽的就将,下面这句代码放开即可 mRingPaint.setStrokeCap(Paint.Cap.ROUND);// ...
- android多线程进度条
多线程实现更新android进度条. 实例教程,详细信息我已经注释 android多线程进度条 01package com.shougao.hello; 02 03import android ...
- Android中ProgressBar的使用-通过Handler与Message实现进度条显示
场景 进度条效果 注: 博客: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实现 将布局改为 ...
- android——利用SharedPreference做引导界面
很久以前就接触过sharedPreference这个android中的存储介质.但是一直没有实际使用过,今天在看之前做的“民用机型大全”的app时,突然想到可以使用sharedPreference类来 ...
- android 对话框中的进度条 (ProgressDialog)
from:http://byandby.iteye.com/blog/817214 显然要定义对话框进度条就要用ProgressDialog,首先我们需要创建ProgressDialog对象,当然这里 ...
- MFC3 基本对话框的使用(三) 滑块与进度条(sdnu)(C++大作业)
一.完成界面 运行前: 运行后: 二.工具 (1)滑块 (2)进度条 (3)文本框 (4)文本示例 (5)按钮 三.添加变量 四.添加事件 右键单击主对话框空白部分,打开类向导,选择"消息& ...
- Android自定义一款带进度条的精美按键
Android中自定义View并没有什么可怕的,拿到一个需要自定义的View,首先要做的就是把它肢解,然后思考每一步是怎样实现的,按分析的步骤一步一步的编码实现,最后你就会发现达到了你想要的效果.本文 ...
- [置顶] 自定义的解压进度条 关于ProgressBar的使用
整体布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android ...
- Android BGradualProgress 多种渐变、直角or弧角、进度条、加载条
可实现多种渐变.直角or弧角.进度条.加载条 (Various gradient, right or arc angle, progress bar and loading bar can be re ...
随机推荐
- Linux-Memcache和Redis常用命令
Memcache: 支持类型: String add, delete, set, replace, get, flush_all, stats, stats reset, stats i ...
- ACM遇到的问题与解决方案
C++防止栈溢出措施: 只要在你的代码里加上下面这句话, OK,栈溢出直接搞定!!! #pragma comment(linker, "/STACK:102400000,102400000& ...
- dokuwiki语法
dokuwiki是一个php写的维基系统,它的插件中包含markdown插件.但是markdown语法跟dokuwiki语法混着用会出现一些bug.所以还是学一下dokuwiki的语法吧. dokuw ...
- java.sql.SQLException: 索引中丢失 IN 或 OUT 参数:: 1
String sql1 = "insert into TEST_RELEVANCEEXPORT" + " (ID, YYCSDM, YYCSMC, ...
- 在Android 开发中使用 SQLite 数据库笔记
SQLite 介绍 SQLite 一个非常流行的嵌入式数据库,它支持 SQL 语言,并且只利用很少的内存就有很好的性能.此外它还是开源的,任何人都可以使用它.许多开源项目((Mozilla, PH ...
- 设置Adobe Reader打开PDF文件保持记忆功能
设置Adobe Reader打开PDF文件保持记忆功能 打开菜单“编辑”->“首选项”. 选择种类中的“文档”,在“打开设置”区域勾上“重新打开文档时恢复上次视图设置(R)”,确定之后就可以在下 ...
- AP_自动付款工作台设定和操作(流程)
2014-06-04 Created By BaoXinjian
- Python2 字典 has_key() 方法
描述 Python2 字典 has_key() 方法用于判断键(key)是否存在于字典(D)中,如果键在字典中返回True,否则返回False. 官方文档推荐用 in 操作符,因为它更短更通俗易懂.h ...
- debian系在线安装软件apt-get命令族
一.背景 apt-get install/remove在线安装/卸载文件真是方便极了. 但是有时候安装/卸载文件不清楚文件在服务器上的实际命名,例如想安装sndfile.应该执行下面哪个命令呢? ap ...
- 谱聚类(Spectral clustering)(1):RatioCut
作者:桂. 时间:2017-04-13 19:14:48 链接:http://www.cnblogs.com/xingshansi/p/6702174.html 声明:本文大部分内容来自:刘建平Pi ...