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

时间:2014-02-04 19:03:31

标签: java android xml textview android-dialog

所以我试图在一个类中创建两个对话框 dialogInfo & dialogGuide )。一个用于显示有关应用程序的信息,另一个用于显示指南如何使用它。我可以通过分别在主类( startover )上单击图像按钮( imbtnInfo & imbtnGuide )来访问它们

信息对话框工作正常。但我无法在“指南”对话框中使用textview,因此现在成为我的问题。 我想在指南对话框中显示来自/res/raw/legal.txt的文本文件(我创建了原始文件夹)

我的文字文件也包含HTML代码。

以下是我的代码:

startover.java(主要类)

    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 txt;

@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(Context); 
   dialogGuide.setContentView(R.layout.guide);
   dialogGuide.setTitle("Guide");
   dialogGuide.setCancelable(true);

   txt = (TextView)findViewById(R.id.textbox);
   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) {
           txt.setText(readRawTextFile(R.raw.legal));
           dialogInfo.show();
       }
   });

}



}

info.xml(对于 dialogInfo

<?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" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/textbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Mobile Medical Record "
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textSize="20dp" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="1.0.0"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="Compatible with : Android 4.0.0 and up"
            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="Mobile Medical Record is an application supported by a hardware that able to display and save data of your heart and respiratory rate."
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="20dp"
            android:text="Copyright © 2014"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/TextView02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="0dp"
            android:text="Fitria Handayani"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/TextView03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="0dp"
            android:text="All rights reserved."
            android:textAppearance="?android:attr/textAppearanceSmall" />

    </LinearLayout>
 </ScrollView>

</LinearLayout>

guide.xml(适用于 dialogGuide 不是最终的布局,但决赛将是这样的

<?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>

由于我是Android应用程序制作的新手(不到一周),请帮助我。提前感谢任何试图帮助我的人&gt;。&lt;

已编辑: 根据Sagar的建议编辑的Java代码:

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;


 }   
}

1 个答案:

答案 0 :(得分:0)

  

我无法在“指南”对话框中使用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;
}