Java 在一个类中设置多个对话框,并在其中一个对话框上设置textview

Java 在一个类中设置多个对话框,并在其中一个对话框上设置textview,java,android,xml,textview,android-dialog,Java,Android,Xml,Textview,Android Dialog,所以我试着在一节课上制作两个对话框dialogInfo和dialogGuide。一个用于显示有关应用程序的信息,另一个用于显示如何使用它的指南。我可以通过分别单击主类startover上的图像按钮imbtnInfo和imbtnGuide来访问这两个页面 信息对话框工作正常。但我无法在向导对话框中使用textview,所以现在这成了我的问题。我想在创建raw文件夹的向导对话框中显示/res/raw/legal.txt中的文本文件 我的文本文件也包含html代码 这是我的密码: java是主类 di

所以我试着在一节课上制作两个对话框dialogInfo和dialogGuide。一个用于显示有关应用程序的信息,另一个用于显示如何使用它的指南。我可以通过分别单击主类startover上的图像按钮imbtnInfo和imbtnGuide来访问这两个页面

信息对话框工作正常。但我无法在向导对话框中使用textview,所以现在这成了我的问题。我想在创建raw文件夹的向导对话框中显示/res/raw/legal.txt中的文本文件

我的文本文件也包含html代码

这是我的密码:

java是主类

dialogInfo的info.xml

我无法在指南对话框中使用textview,因此它成为我的首选 现在有问题了

因此,以下是您的解决方案:

TextView mtxt1 = (TextView)dialogGuide.findViewById(R.id.textView1);
mtxt1.setText(ImportTextFile());
用于从文件中读取文本:

String ImportTextFile()
{
    String myText = "";
    try {
    InputStream fileStream = getResources().openRawResource(
                        R.raw.legal);
    int fileLen = fileStream.available();
    // Read the entire resource into a local byte buffer.
    byte[] fileBuffer = new byte[fileLen];
    fileStream.read(fileBuffer);
    fileStream.close();
    displayText = new String(fileBuffer);
    } catch (IOException e) {
      // exception handling
    }
    return myText;
}

如果我没弄错的话,那就意味着我必须把所有的文本都写在代码上,对吗?但是我的文本太长了,所以我想从.txt文件中“导入”它。你最好尝试另一种方式。否则,您也可以从.txt文件复制整个文本,并将其粘贴到相应的文本视图中。它说settextstring对于TextView是未定义的,您能告诉我应该在哪个代码之后放置它们吗,因为现在有很多错误?我的代码中的两行TextView都在您的dialogGuide.setCancelabletrue;下方法导入OnCreate方法下面的ImportTextFile。
<?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" >

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="In order to operate this application and device you must sign up and fill information about yourself; name and age."
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/TextView01"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:gravity="fill"
            android:text="There are two function in Mobile Medical Record, they are 'Record' and 'Data'. Record enables you to communicate with the hardware supporting this application so you will get results of your heart rate and respiratory measurement. Data enables you to save last 10 measurement of your heart rate and respiratory rate."
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Record"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/textbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

    </LinearLayout>
</ScrollView>

</LinearLayout>
package com.fitria.MedicalRecord;


import com.fitria.cobalogin.R;

import android.content.Context;
import android.widget.ImageButton;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.text.Html;
import android.text.util.Linkify;

public class startover extends Activity
{
ImageButton imbtnInfo, imbtnGuide;
Button btnRecord, btnData, btnExit;
Dialog dialogInfo, dialogGuide;
TextView mtxt1;


@Override
public void onCreate(Bundle savedInstanceState) {         

super.onCreate(savedInstanceState);



   setContentView(R.layout.startover);
   dialogInfo = new Dialog(this); 
   dialogInfo.setContentView(R.layout.info);
   dialogInfo.setTitle("Info");
   dialogInfo.setCancelable(true);

   dialogGuide = new Dialog(this); 
   dialogGuide.setContentView(R.layout.guide);
   dialogGuide.setTitle("Guide");
   dialogGuide.setCancelable(true);
   mtxt1 = (TextView)dialogGuide.findViewById(R.id.textbox);
   mtxt1.settext(ImportTextFile());

   imbtnInfo=(ImageButton)findViewById(R.id.imageInfo);
   imbtnGuide=(ImageButton)findViewById(R.id.imageGuide);
   btnRecord=(Button)findViewById(R.id.buttonRecord);
   btnData=(Button)findViewById(R.id.buttonData);
   btnExit=(Button)findViewById(R.id.buttonExit);





   btnRecord.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // TODO Auto-generated method stub

        /// Create Intent for SignUpActivity  and Start The Activity
        Intent intentSignUP=new         Intent(getApplicationContext(),signup.class);
        startActivity(intentSignUP);
        }
    });

   btnData.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // TODO Auto-generated method stub

            /// Create Intent for SignUpActivity  and Start The Activity
            Intent intentSignUP=new Intent(getApplicationContext(),signup.class);
            startActivity(intentSignUP);
            }
        });
   btnExit.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // TODO Auto-generated method stub

            /// Create Intent for SignUpActivity  and Start The Activity
            Intent intentHome=new Intent(getApplicationContext(),Home.class);
            startActivity(intentHome);
            }
        });
   imbtnInfo.setOnClickListener(new View.OnClickListener() {
       public void onClick(View v) {
           dialogInfo.show();
       }
   });
   imbtnGuide.setOnClickListener(new View.OnClickListener() {
       public void onClick(View v) {

           dialogInfo.show();
       }
   });

}



String ImportTextFile()
{
       String myText = "";
       try 
       {
       InputStream fileStream = getResources().openRawResource(R.raw.legal);
       int fileLen = fileStream.available();
       // Read the entire resource into a local byte buffer.
       byte[] fileBuffer = new byte[fileLen];
       fileStream.read(fileBuffer);
       fileStream.close();
       displayText = new String(fileBuffer);
       }
       catch (IOException e) 
       {
         // exception handling
       }
       return myText;


 }   
}
TextView mtxt1 = (TextView)dialogGuide.findViewById(R.id.textView1);
mtxt1.setText(ImportTextFile());
String ImportTextFile()
{
    String myText = "";
    try {
    InputStream fileStream = getResources().openRawResource(
                        R.raw.legal);
    int fileLen = fileStream.available();
    // Read the entire resource into a local byte buffer.
    byte[] fileBuffer = new byte[fileLen];
    fileStream.read(fileBuffer);
    fileStream.close();
    displayText = new String(fileBuffer);
    } catch (IOException e) {
      // exception handling
    }
    return myText;
}