安卓查看特定文件,并按钮查看下一个或上一个

来源:互联网 发布:mysql高级视频教程 编辑:程序博客网 时间:2024/06/06 13:57
为下一篇打底
import android.app.Activity;import android.os.Bundle;import android.os.Environment;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;import java.io.File;import static android.widget.Toast.LENGTH_SHORT;/** * Created by 允琪 on 2017/5/14. */public class NextFile extends Activity{    public String sdPath = "/sdcard/File/";    public File folder;    public String [] allFile;    public String scanpath;    private Button zhege,xiage;    private TextView textView;    private int flag = 0;    protected void onCreate(Bundle savedInstanceState){        super.onCreate(savedInstanceState);        setContentView(R.layout.nextfile_layout);        zhege = (Button) findViewById(R.id.zhege);        xiage = (Button) findViewById(R.id.xiage);        textView = (TextView)findViewById(R.id.text);        folder = new File(sdPath);        allFile = folder.list();        final String a = allFile[1];        for (int i = 0 ;i < allFile.length;i++){            scanpath = Environment.getExternalStorageState().toString()+sdPath+allFile[i];            Log.d("scanpath",scanpath);        }        zhege.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                scanpath = Environment.getExternalStorageState().toString()+sdPath+allFile[flag];                Toast.makeText(NextFile.this,"当前位于 文件"+allFile[flag],Toast.LENGTH_SHORT).show();            }        });        xiage.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                ++flag;                scanpath = Environment.getExternalStorageState().toString()+sdPath+allFile[flag];                Toast.makeText(NextFile.this,"当前位于 文件"+allFile[flag],Toast.LENGTH_SHORT).show();            }        });    }}
layout文件
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:layout_width="match_parent"              android:layout_height="match_parent"    android:orientation="vertical">    <Button        android:text="This"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginBottom="238dp"        android:id="@+id/zhege"        android:layout_alignParentBottom="true"        android:layout_toLeftOf="@+id/text"        android:layout_toStartOf="@+id/text"        android:layout_marginRight="34dp"        android:layout_marginEnd="34dp"/>    <Button        android:text="Next"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginRight="31dp"        android:layout_marginEnd="31dp"        android:id="@+id/xiage"        android:layout_alignBaseline="@+id/zhege"        android:layout_alignBottom="@+id/zhege"        android:layout_alignParentRight="true"        android:layout_alignParentEnd="true"/>    <TextView        android:text="TextView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/text"        android:layout_above="@+id/zhege"        android:layout_centerHorizontal="true"        android:layout_marginBottom="64dp"/></RelativeLayout>


 
原创粉丝点击