【问题标题】:Generate random XML document生成随机 XML 文档
【发布时间】:2016-09-25 11:49:15
【问题描述】:

我尝试创建一个有一些问题的应用程序。我想要按钮生成随机问题,但到目前为止我找不到任何东西(如果可能的话,一些更短的解决方案)。谢谢你:)

something.java 文件代码:

public class something extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.somethingXML);

}
public void generate (View view){
    Intent intent = new Intent(this, ???? .class);
    startActivity(intent);
}}

somethingXML.xml 布局文件代码:

<Button
        android:layout_width="fill_parent"
        android:layout_height="30pt"
        android:text="@string/buttonM"
        android:id="@+id/buttonM"
        android:layout_marginTop="46dp"
        android:onClick="generate"
        android:clickable="true"
        android:textStyle="bold" />

如果我使用 id 为“generate”的按钮,我想生成它随机的 xml 页面,但 Intent(this, ???? .class) 只有一个选项 [示例:Intent(this, question1.class) ],但我不希望用户总是打开这个应用程序来解决问题 1 页面。

【问题讨论】:

    标签: java android xml android-studio random


    【解决方案1】:

    如果您有 10 个问题,请编写以下代码生成random numbers from 1 to 10, and then call yourintentinswitch case`

    Random r = new Random();
    int randNum = r.nextInt(10 - 1) + 1;
    Intent intent = null;
    
    switch(randNum) {
        case 1  :intent = new Intent(this, question1.class); break;
        case 2  :intent = new Intent(this, question2.class); break;
        case 3  :intent = new Intent(this, question3.class); break;
        .
        .
        .
        case 10 :intent = new Intent(this, question9.class); break;
        default :intent = new Intent(this, question1.class); break;
    }
    startActivity(intent);
    

    【讨论】:

    • 遗憾的是,我预计大约有 100 个问题......但如果没有其他帮助,我将使用它。谢谢!
    • @Jiří 你也可以用它来回答 100 个问题。但这不是完成任务的好主意。您可以针对单个活动生成问题,无需创建 100 个活动。
    • 无论如何,上面的这个解决方案在任何时候都有一些帧问题它会生成新的链接(在 API 中):09-27 18:48:02.938 2502-2646/com.example.ji.myapplication I/OpenGLRenderer :初始化 EGL,版本 1.4 09-27 18:48:03.738 2502-2502/com.example.ji.myapplication I/Choreographer:跳过 40 帧!应用程序可能在其主线程上做了太多工作。 09-27 18:49:51.788 2502-2646/com.example.ji.myapplication E/Surface:getSlotFromBufferLocked:未知缓冲区:0xae4312a0 09-27 18:49:53.461 2502-2646/com.example.ji.myapplication(和等等)
    • 好吧,如果我在上面使用这个方法,它会以某种方式工作,但如果它有 10 个问题,想看看它如何与另外 90 个问题一起工作......所以其他东西可能会更好...... .
    • @Jiří 我试图回答你的问题。你得到的错误与我的回答无关
    猜你喜欢
    • 2011-02-17
    • 1970-01-01
    • 2014-07-02
    • 1970-01-01
    • 2020-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多