简体   繁体   English

如何在android studio中有效显示正确的弹出窗口

[英]how to efficiently display the correct pop up window in android studio

Backstory: I'm making a very simple Rock paper scissors app. 背景故事:我正在制作一个非常简单的剪刀石头布应用程序。 In the app you have 3 buttons to select either rock, paper, or scissors. 在应用程序中,您有3个按钮可以选择石头,纸张或剪刀。

When you select the rock, I have a popupwindow that says "Computer plays paper you lose" 当您选择岩石时,我有一个弹出窗口,上面写着“计算机丢失了纸张”

The problem is this popup message needs to be dynamic. 问题在于此弹出消息需要动态。

My goal is to have the computer randomly select a move and determine the outcome of the game which is display in a popupwindow. 我的目标是让计算机随机选择一个动作并确定显示在弹出窗口中的游戏结果。 So for example if I choose rock and the computer chooses scissors the popup would generate the message "Computer selects scissors you win!" 因此,例如,如果我选择岩石,而计算机选择剪刀,则弹出窗口将生成消息“计算机选择您将赢得的剪刀!”。 This is probably pretty basic to all of you but I'm still getting familiar with android development and android studio. 这可能对你们所有人来说都是很基本的,但是我仍然对android开发和android studio熟悉。

Is it possible to have just one popupwindow.xml file and pass values to display the correct message? 是否可能只有一个popupwindow.xml文件并传递值以显示正确的消息? Like "Computer plays 'computermov' you 'result'!" 就像“计算机播放'computermov'就是您'结果'!”

How can I implement this? 我该如何实施? Below is page2.java which has only one of the buttons programmed to call the popupwindow. 下面是page2.java,其中只有一个按钮被编程为调用popupwindow。 I need to make it work for all three buttons. 我需要使它适用于所有三个按钮。 I also included popupwindow.xml and Pop.java, a class I made that calls the popupwindow. 我还包括popupwindow.xml和Pop.java,这是我制作的一个名为popupwindow的类。

popupwindow.xml popupwindow.xml

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Computer played Paper you lose!"
    android:id="@+id/textView"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

page2.java page2.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activitypage2);

ImageButton btn = (ImageButton) findViewById(R.id.imageButtonRock);
    //btn is set to the rock image needs to be dynamic
btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        startActivity(new Intent(page2.this, Pop.class));
    }
});
}

Pop.java 流行的

public class Pop extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.popupwindow);

    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);

    int width = dm.widthPixels;
    int height = dm.heightPixels;

    getWindow().setLayout((int)(width*.8),(int)(height*.4));
}

So what I'm thinking is that in popupwindow.xml I could hardcode all possible outcomes of the computer selection and result of the game and just display the correct popup by id at runtime. 所以我在想,我可以在popupwindow.xml中对所有可能的计算机选择结果和游戏结果进行硬编码,并在运行时按ID显示正确的弹出窗口。 Seems like the wrong way to do it but I think that could work? 似乎是错误的做法,但我认为这样可行吗?

Whats a more efficient way of doing this? 什么是更有效的方法? Thanks in advance! 提前致谢!

It sounds like you might want to use an AlertDialog , eg 听起来您可能想使用AlertDialog ,例如

String  computerSelected = ...;
boolean computerWon = ...;
new AlertDialog.Builder(context)
    .setMessage("Computer selected " + computerSelected + ". You " + (computerWon ? "loose" : " win"))
    .show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM