所以我试图在一个类中创建两个对话框(dialogInfo 和dialogGuide)。一个用于显示有关应用程序的信息,另一个用于向指南显示如何使用它。我可以通过分别单击主类上的图像按钮(imbtnInfo 和 imbtnGuide)来访问它们(startover)
信息对话框工作正常。但是我无法在“指南”对话框中使用 TextView ,所以它现在成为我的问题。 我想在“指南”对话框上显示/res/raw/legal.txt 中的文本文件(我创建了 raw 文件夹)
我的文本文件也包含 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 应用程序制作非常陌生(不到一周),请帮助我。预先感谢任何试图帮助我的人>.<
已编辑: 根据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;
}
}
最佳答案
I can't achieve to use textview in Guide dialog so it becomes my problem now.
这是您的解决方案:
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;
}
关于java - 在一个类中设置多个对话框并在其中一个对话框上设置 TextView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21561129/