简体   繁体   English

Android Studio-如何通过单击Button1和Button2分别单击资产文件夹中的文本页面A和B

[英]Android studio- How to display text page A and B from assets folder in a click of Button1 and Button2 resp

While executing im getting error like "Unfortunately your app is stopped". 执行即时讯息时,出现类似“不幸的是您的应用已停止”的错误。 I want to open my text or html file once the button is clicked. 单击按钮后,我想打开我的文本或html文件。

MainActivity.java MainActivity.java

package com.example.akarsh.aaa;

import android.graphics.drawable.Drawable;

import java.io.InputStream;

import android.os.Bundle;
import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

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

        TextView txtContent = (TextView) findViewById(R.id.tv1);
        TextView txtFileName = (TextView) findViewById(R.id.tv2);
        ImageView iv = (ImageView) findViewById(R.id.iv);

        AssetManager assetManager = getAssets();

        try {

            String[] files = assetManager.list("Files");

            for (int i = 0; i < files.length; i++) {
                txtContent.append("\n Files=>" + i + "Name" + files);
            }
        } catch (Exception e) {
            // TODO: handle exception

            e.printStackTrace();
        }

        InputStream input;

        try {

            input = assetManager.open("text.txt");

            int size = input.available();

            byte[] buffer = new byte[size];
            input.read(buffer);
            input.close();

            String text = new String(buffer);

            txtFileName.setText(text);

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

        try {

            InputStream in = assetManager.open("icon_android_small.png");

            Drawable d = Drawable.createFromStream( in , null);
            iv.setImageDrawable(d);

        } catch (Exception e) {
            // TODO: handle exception
            return;
        }
    }
}

You need to implement the onClick-Method on your button 您需要在按钮上实现onClick-Method

Find the button in the activity_main -File and set activity_main -File中找到按钮并设置

android:onClick="onClick"

Then implement the onClick in the sourceCode of your MainActivity. 然后在MainActivity的sourceCode中实现onClick。 Like: 喜欢:

public void onClick(View view){
...
    }

And in this method you open your file. 然后用这种方法打开文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击Button1然后单击Button2会生成一个“字母或字符”-Android - Button1 and then Button2 when click it will generate one “letter or character” - Android Android Studio-单击按钮可多次更改按钮文本 - Android Studio- Click button to change the buttons text multiple times Android Studio - 如果按下按钮且文本框中没有文本,则显示一个字符串,输入文本时显示另一个字符串 - Android Studio- Display one string if a button is pressed with no text in the textbox and another when text is inputted 单击Button1和Button2将根据Java中的顺序更改为多种颜色 - Click Button1 and Button2 will change to multiple colour according to order in Java Button2正在做button1应该做的 - Button2 is doing what button1 should be doing 单击按钮访问资产文件夹中的图像 - access images from assets folder with a button click Android应用程序在移动设备上单击button1时崩溃,但在模拟器中成功运行 - Android app crashes on button1 click in mobile but it runs successfully in emulator Android Studio-轻按按钮即可播放声音 - Android Studio- Play Sound on button tap WEAR Android Studio - 为什么这个图像按钮会关闭我的应用程序? - Android Studio- Why is this image button closing my app? Android studio-当互联网不可用时禁用下载按钮/活动 - Android studio- disable donwload button/Activity when internet not availabe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM