如何在Android Studi中制作一个下载pdf文件的下载按钮

时间:2017-04-30 08:16:42

标签: android-studio button download

我正在创建一个应用程序,我想要一个允许用户下载我的文件的下载按钮。那么我该如何制作这样的按钮?

2 个答案:

答案 0 :(得分:0)

这是两个问题......
首先你需要一个普通的Button [在yourlayoutfile.yml中执行此操作]

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_test"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="198dp"
        android:text="Download" />
</RelativeLayout>

现在在你的Button中为你的Button设置一个OnClickListener:

public class MainActivity extends AppCompatActivity {

    private Button downloadButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        downloadButton= (Button) findViewById(R.id.button);
        downloadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               downloadFile();
            }
        });
    }

    public void downloadFile() {
        //Here put-in youre download stuff
        //so download the file from your server
    }
}

希望这会有所帮助:)

答案 1 :(得分:0)

创建void downloadFile(URI uri)你必须使用URI示例 -

    DownloadManager downloadManager = (DownloadManager) 
getActivity().getSystemService(Context.DOWNLOAD_SERVICE);

        DownloadManager.Request request = new DownloadManager.Request(uri);

        request.setTitle("your tittle");
        request.setDescription("your description");
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);


        Long reference = downloadManager.enqueue(request);

在最后设置onclick方法并写

Uri urifile = Uri.parse("your file address");
downloadfile(urifile);