我有一个关于Android Studio的简单问题。我正在尝试在Android Studio上制作一个互动故事,但我不想让自己制作过多的活动。如何使背景更改并且textView更改但在按下按钮时保持在同一活动中?
答案 0 :(得分:0)
制作互动故事而不进行多项活动
现在我将向您展示如何制作简单的交互式故事应用程序的简单演示。您必须确保阅读我添加到我的代码中的所有注释,以便您可以理解这个概念强>
应用程序的工作原理:您有一个带有textview和两个按钮的活动.textView显示故事并点击按钮用新故事取代故事
在您的activity_main.xml文件中添加一个textView和两个按钮。就像这个
<TextView //Shows the story
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/storyTextView"/>
<Button //navigate through different story
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/choiceButton2"/>
<Button //navigate through different story
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/choiceButton1"/>
现在你必须制作三个模型类。
Choice.java,Page.java和Story.java
Choice.java
public class Choice {
private String mText; //The text that will show on the button
private int mNextPage; //mNextPage store the Next Page number If a user click on this button then which story should load?
public Choice(String text, int nextPage) { //Constructor
mText = text;
mNextPage = nextPage;
}
//Add getter and setter by pressing ALT+ENTER and select getter and setter and select all and OK
}
Page.java
public class Page {
private String mText; //Store the Story
private Choice mChoice1; //Remember in our Choice.java class.
private Choice mChoice2; //Remember in our Choice.java class.
public Page(String text, Choice choice1, Choice choice2) {
mText = text;
mChoice1 = choice1;
mChoice2 = choice2;
}
//Add getter and setter by pressing ALT+ENTER and select getter and setter and select all and OK
}
Story.java
public class Story { //Here we use previously made model class and store our story and navigate through functionality.
private Page[] mPages;
public Story() {
mPages = new Page[5]; //Suppose I have 5 story.So I made 5 array of Page.java
mPages[0] = new Page(
"Story 1",
new Choice("Navigate to the story 1", 1),
new Choice("Navigate to the story 2", 2));
mPages[1] = new Page(
"Story 2",
new Choice("Navigate to the story 3", 3),
new Choice("Navigate to the story 4", 4));
mPages[2] = new Page(
"Story 3",
new Choice("Navigate to the story 4", 4),
new Choice("Navigate to the story 6", 6));
mPages[3] = new Page(
"Story 4",
new Choice("Navigate to the story 4", 4),
new Choice("5", 5));
mPages[4] = new Page(
"Story 5",
new Choice("Navigate to the story 5", 5),
new Choice("Navigate to the story 6", 6));
}
public Page getPage(int pageNumber) {
return mPages[pageNumber]; //This method will called in MainActivity to get the appropriate Page Object.
}
}
现在,您需要在MainActivity中添加此
public class MainActivity extends Activity {
private Story mStory = new Story();
private TextView mTextView;
private Button mChoice1;
private Button mChoice2;
private Page mCurrentPage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_story);
mImageView = (ImageView)findViewById(R.id.storyImageView);
mTextView = (TextView)findViewById(R.id.storyTextView);
mChoice1 = (Button)findViewById(R.id.choiceButton1);
mChoice2 = (Button)findViewById(R.id.choiceButton2);
loadPage(0);//When we launched our app it will show the story of **mPages[0]**
}
private void loadPage(int choice) {
mCurrentPage = mStory.getPage(choice);
String pageText = mCurrentPage.getText();
mTextView.setText(pageText);
mChoice1.setText(mCurrentPage.getChoice1().getText());
mChoice2.setText(mCurrentPage.getChoice2().getText());
//Now we are setting onClickListenere For each button and replace the story with new story
mChoice1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int nextPage = mCurrentPage.getChoice1().getNextPage();//getting which next story
loadPage(nextPage);// and replace with old story
}
});
mChoice2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int nextPage = mCurrentPage.getChoice2().getNextPage();
loadPage(nextPage);
}
});
}
}
这就是全部。我最好地尝试了我的标签。希望得到这个帮助。