我目前正在对我正在制作的应用程序进行最后润色,该应用程序基本上是MadLibsStory应用程序。对于任何不知道那是什么的人……我本质上是在要求用户输入一堆单词来填写他们尚未看到的故事。只需提示他们输入的单词类型(adj,名词,复数名词等),然后当所有占位符都填满时,就会向用户显示故事,并将他们输入的单词放在占位符所在的位置形成一个古怪的故事
我有3个活动:
1)带按钮的欢迎屏幕。用户按下按钮开始输入单词。
2)单词输入屏幕,供用户输入单词,直到getRemainingPlaceholders()方法返回0
3)从Activity中检索完成的MadLib对象,并在TextView中为用户显示该对象,并将他们输入的所有单词填入占位符所在的位置。
我想做的是找到一种方法,使他们在活动3中显示时在活动2中输入的单词变粗(仅在活动2中作为占位符输入的单词,而不是其余的故事)。这样,用户可以轻松查看自己的单词,因为它们将以粗体显示!
除此之外,一切都已经开始了。这是我的代码:
MainActivity
package com.example.assignment2;
import android.content.Intent;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.IOException;
import java.io.InputStream;
/**
* This is the main activity for the application and will display the welcome screen.
*/
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view){
Intent i = new Intent(this, MadLibEntry.class);
startActivity(i);
}
}
MadLibEntry.java
public class MadLibEntry extends AppCompatActivity {
//empty madlib object
MadLibStory mlStory = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mad_lib_entry);
//gets intent from main activity
getIntent();
Resources resources = getResources();
String fileName = resources.getString(R.string.university);
AssetManager assetManager = getAssets();
try {
//populates madlib object with given file text
InputStream inputStream = assetManager.open(fileName);
mlStory = new MadLibStory(inputStream);
}catch(IOException ioe){
}
//text view to display first word type (adjective, noun, etc.)
TextView wordTypeView = (TextView) findViewById(R.id.wordType);
wordTypeView.setText(mlStory.getNextPlaceholder());
}
//when the submit button is clicked
public void onSubmit(View view){
//text view to display word type (adjective, noun, etc.)
TextView wordTypeView = (TextView) findViewById(R.id.wordType);
//editText reference
EditText wordEntryView = (EditText) findViewById(R.id.wordEntry);
//sets the edittext view as the input the user gave
wordEntryView.getText();
mlStory.fillInPlaceholder(wordEntryView.getText().toString());
//sets the word type for the user (adjective, noun, etc.)
wordTypeView.setText(mlStory.getNextPlaceholder());
//Starts the next activity if all of the placeholders have been filled
if(mlStory.getPlaceholderRemainingCount() == 0) {
//sending intent to next activity
Intent ii = new Intent(this, MadLibResult.class);
ii.putExtra("mlStory", mlStory);
startActivity(ii);
}
}
}
MadLibResult.java
public class MadLibResult extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mad_lib_result);
//getting intent sent from activity 2
Intent i = getIntent();
//creating a new MadLib object and populating it with the text recieved from activity 2
MadLibStory finalStory = (MadLibStory) i.getSerializableExtra("mlStory");
//Textview Reference for story display
TextView resultView = (TextView)findViewById(R.id.resultView);
//display story to user using textview
resultView.setText(finalStory.toString());
}
public void onClick(View view){
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
}
}
MadLibStory.java
public class MadLibStory extends AppCompatActivity implements Serializable {
private String text; // text of the Mad Lib
private List<String> placeholders; // list of placeholders to fill in
private int filledIn; // number of placeholders that have been filled in
private boolean htmlMode; // set to true to surround placeholders with <b></b> tags
/** constructs a new empty MadLibStory **/
public MadLibStory() {
init();
}
/** constructs a new MadLibStory reading its text from the
given input stream **/
public MadLibStory(InputStream stream) {
init();
read(stream);
}
/** constructs a new MadLibStory reading its text from the
given Scanner **/
public MadLibStory(Scanner input) {
init();
read(input);
}
/** initializes the attributes for the class **/
private void init(){
text = "";
placeholders = new ArrayList<String>();
filledIn = 0;
htmlMode = false;
}
/** resets the MadLibStory back to an empty initial state **/
public void clear() {
text = "";
placeholders.clear();
filledIn = 0;
}
/** replaces the next unfilled placeholder with the given word **/
public void fillInPlaceholder(String word) {
if (!isFilledIn()) {
text = text.replace("<" + filledIn + ">", word);
filledIn++;
}
}
/** returns the next placeholder such as "adjective",
* or empty string if MadLibStory is completely filled in already **/
public String getNextPlaceholder() {
if (isFilledIn()) {
return "";
} else {
return placeholders.get(filledIn);
}
}
/** returns total number of placeholders in the MadLibStory **/
public int getPlaceholderCount() {
return placeholders.size();
}
/** returns how many placeholders still need to be filled in **/
public int getPlaceholderRemainingCount() {
return placeholders.size() - filledIn;
}
/** returns true if all placeholders have been filled in **/
public boolean isFilledIn() {
return filledIn >= placeholders.size();
}
/** reads initial Mad Lib Story text from the given input stream **/
public void read(InputStream stream) {
read(new Scanner(stream));
}
/** reads initial Mad Lib Story text from the given Scanner **/
public void read(Scanner input) {
while (input.hasNext()) {
String word = input.next();
if (word.startsWith("<") && word.endsWith(">")) {
// a placeholder; replace with e.g. "<0>" so I can find/replace it easily later
// (make them bold so that they stand out!)
if (htmlMode) {
text += " <b><" + placeholders.size() + "></b>";
} else {
text += " <" + placeholders.size() + ">";
}
// "<plural-noun>" becomes "plural noun"
String placeholder = word.substring(1, word.length() - 1).replace("-", " ");
placeholders.add(placeholder);
} else {
// a regular word; just concatenate
if (!text.isEmpty()) {
text += " ";
}
text += word;
}
}
}
/** returns MadLibStory text **/
public String toString()
{
return text;
}
}