简体   繁体   English

Android Studio:读取文本文件,并在弹出窗口中显示

[英]Android Studio: Read in a text file, display in pop-up

I'm creating an app that contains a "Tip of the Day" feature. 我正在创建一个包含“每日提示”功能的应用。 This is essentially a pop-up, activated by a button. 这实际上是一个弹出窗口,由按钮激活。 It currently has filler text right now, but I am trying to create a way in which a text file (stored in src/main/assets) is read, and an individual line is displayed in the pop-up. 目前,它当前具有填充文本,但是我试图创建一种方式来读取文本文件(存储在src / main / assets中),并在弹出窗口中显示一行。 How can I do this? 我怎样才能做到这一点? These lines in the text file are individualized by the return key. 文本文件中的这些行由返回键个性化。 I will find a way to display unique tips each time the button is clicked, but I will get to that part later. 每次单击按钮时,我都会找到一种显示唯一提示的方法,但稍后我会介绍该部分。

Here is the code for the pop-up itself: 这是弹出窗口的代码:

public class homeFragmentDialog extends Activity {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
    builder1.setMessage("Filler text.");
    builder1.setCancelable(true);

    builder1.setPositiveButton(
            "Close",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });

    AlertDialog alert11 = builder1.create();
    alert11.show();

}
}

And just in case, here is the fragment file which holds the button that activates the previous activity: 以防万一,这里是片段文件,其中包含激活上一个活动的按钮:

public class homeFragment extends Fragment {

View rootView;

private Button button0;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_home, container, false);

    button0 = (Button) rootView.findViewById(R.id.buttonDialog);
    button0.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), homeFragmentDialog.class);
            startActivity(intent);
        }


    });

    return rootView;

}

}

This should be enough for you to fetch the information from the text file in the assets folder as String. 足以让您从资产文件夹中的文本文件中以字符串形式获取信息。

If you have problems with this short code snippet this one should be way more extensive. 如果你有这短暂的代码段的问题这一个应该是方式更加广泛。

Once you got the text as a String object you should be able to add it inside your setMessage function to display it. 将文本作为String对象后,您应该能够将其添加到setMessage函数中以显示它。

You can use getAssets() if you access to a Context: 如果您访问Context,则可以使用getAssets()

try {
    InputStream inputStream = getAssets().open("textfile.txt");
    builder1.setMessage(convertStreamToString(inputStream));
} catch (IOException e){
    // Log exception
}

Where convertStreamToString() came from Pavel Repin's answer : convertStreamToString()来自Pavel Repin的答案

static String convertStreamToString(java.io.InputStream is) {
    java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
    return s.hasNext() ? s.next() : "";
}

Or you can read the ammount of bytes you want(in your case one line). 或者你可以读取你想要的字节数(在你的情况下是一行)。

EDIT: 编辑:

Instead of convertStreamToString(), you can use this method to get a single line: 您可以使用此方法获取单行,而不是convertStreamToString():

static String getLineFromStream(java.io.InputStream is, int linePos){
    BufferedReader br = new BufferedReader(new InputStreamReader(is));

    String line = null;
    try {
        for (int i = 0; i <= linePos; i++) {
            line = br.readLine();
        }
    } catch (IOException e){
        // Handle exception here (or you can throw)
    }

    return line;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM