HtmlTextView是Android的扩展TextView组件,可以通过将HTML组件转换为Android Spannables进行加载以加载HTML组件。除了HTML标记外,该库还允许从本地可绘制文件夹或Internet加载图像,也可以从Internet加载URL。在本文中,我们将使用HTML组件在Android中显示文本,图像和URL。
分步实施
步骤1:创建一个新项目
要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。
步骤2:将其添加到AndroidManifest.xml文件中
将此添加到build.gradle文件中
implementation 'org.sufficientlysecure:html-textview:4.0'
步骤3:使用activity_main.xml文件
导航到应用程序> res>布局> activity_main.xml,然后将以下代码添加到该文件中。以下是activity_main.xml文件的代码。
XML
Java
import android.os.Bundle;
import android.text.Spanned;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import org.sufficientlysecure.htmltextview.HtmlAssetsImageGetter;
import org.sufficientlysecure.htmltextview.HtmlFormatter;
import org.sufficientlysecure.htmltextview.HtmlFormatterBuilder;
import org.sufficientlysecure.htmltextview.OnClickATagListener;
public class MainActivity extends AppCompatActivity {
TextView gtext;
org.sufficientlysecure.htmltextview.HtmlTextView text1, text2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gtext = findViewById(R.id.text);
text1 = findViewById(R.id.text2);
text2 = findViewById(R.id.text3);
Spanned spanned = HtmlFormatter.formatHtml(new HtmlFormatterBuilder().setHtml("
GeeksForGeeks
"));
gtext.setText(spanned);
text2.setHtml("Go to this link");
text2.setOnClickATagListener(new OnClickATagListener() {
@Override
public boolean onClick(View widget, String spannedText, @Nullable String href) {
Toast.makeText(getApplicationContext(), href, Toast.LENGTH_LONG).show();
return false;
}
});
text1.setHtml("Load Image From Asset
", new HtmlAssetsImageGetter(text1));
}
}
步骤4:使用MainActivity。 Java文件
转到MainActivity。 Java文件并参考以下代码。下面是MainActivity的代码。 Java文件
Spanned spanned= HtmlFormatter.formatHtml(new HtmlFormatterBuilder()
.setHtml("GeeksForGeeks
"));
// load text using HtmlFormatterBuilder
text2.setHtml("Go to this link");
text2.setOnClickATagListener(new OnClickATagListener() {
@Override
public boolean onClick(View widget, String spannedText, @Nullable String href) {
Toast.makeText(getApplicationContext(),href,Toast.LENGTH_LONG).show();
return false;
}
});
// loading image using HtmlAssetsImageGetter
text1.setHtml("Load Image From Asset
",new HtmlAssetsImageGetter(text1));
下面是MainActivity的代码。 Java文件。
Java
import android.os.Bundle;
import android.text.Spanned;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import org.sufficientlysecure.htmltextview.HtmlAssetsImageGetter;
import org.sufficientlysecure.htmltextview.HtmlFormatter;
import org.sufficientlysecure.htmltextview.HtmlFormatterBuilder;
import org.sufficientlysecure.htmltextview.OnClickATagListener;
public class MainActivity extends AppCompatActivity {
TextView gtext;
org.sufficientlysecure.htmltextview.HtmlTextView text1, text2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gtext = findViewById(R.id.text);
text1 = findViewById(R.id.text2);
text2 = findViewById(R.id.text3);
Spanned spanned = HtmlFormatter.formatHtml(new HtmlFormatterBuilder().setHtml("
GeeksForGeeks
"));
gtext.setText(spanned);
text2.setHtml("Go to this link");
text2.setOnClickATagListener(new OnClickATagListener() {
@Override
public boolean onClick(View widget, String spannedText, @Nullable String href) {
Toast.makeText(getApplicationContext(), href, Toast.LENGTH_LONG).show();
return false;
}
});
text1.setHtml("Load Image From Asset
", new HtmlAssetsImageGetter(text1));
}
}
输出: