Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android应用程序多视图_Java_Android - Fatal编程技术网

Java Android应用程序多视图

Java Android应用程序多视图,java,android,Java,Android,这是你会发现的最无趣的问题之一,但在我的Android应用程序中,我需要几个不同的“屏幕”;一个您登录的视图,以及一个表示问题的视图(将有多个问题,因此我需要为每个问题创建一个视图,并在单击“下一步”按钮时将其更改为下一个视图。让我用流程图解释: 用户打开应用程序->登录屏幕->用户登录->查看对问题1自动生成视图的更改->单击下一步按钮->问题2->完成所有问题->表示完成的最终视图 我创建了两个xml布局文件,一个名为main,另一个名为quick(表示一个问题) 我将详细说明所需的一切 更

这是你会发现的最无趣的问题之一,但在我的Android应用程序中,我需要几个不同的“屏幕”;一个您登录的视图,以及一个表示问题的视图(将有多个问题,因此我需要为每个问题创建一个视图,并在单击“下一步”按钮时将其更改为下一个视图。让我用流程图解释:

用户打开应用程序->登录屏幕->用户登录->查看对问题1自动生成视图的更改->单击下一步按钮->问题2->完成所有问题->表示完成的最终视图

我创建了两个
xml
布局文件,一个名为main,另一个名为quick(表示一个问题)

我将详细说明所需的一切

更新

以下是我的两个活动课程:

package me.nrubin29.quiz.student;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Main extends Activity {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final Button done = (Button) findViewById(R.id.done);
        final EditText serverAddress = (EditText) findViewById(R.id.serverAddress);
        final EditText secretNumber = (EditText) findViewById(R.id.secretNumber);
        final EditText name = (EditText) findViewById(R.id.name);

        done.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String addr = serverAddress.getText().toString();
                String num = secretNumber.getText().toString();
                String n = name.getText().toString();

                if (addr.equals("") || num.equals("") || n.equals("")) {
                    Toast.makeText(getApplicationContext(), "You didn't fill in all the fields.", Toast.LENGTH_LONG).show();
                    return;
                }

                new Connection().initConnection(Main.this, addr, num, n);
            }
        });
    }
}

下面是我的两个XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >

    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Please enter the server address and secret number your teacher gives you."
            />

    <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/serverAddress" android:layout_gravity="center"
            android:phoneNumber="true"
            android:hint="Server Address"/>
    <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/secretNumber" android:layout_gravity="center"
            android:phoneNumber="true"
            android:hint="Secret Number"/>

    <Space
        android:layout_width="fill_parent"
        android:layout_height="20px"
        android:id="@+id/space1"/>

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Please enter your name."
            android:id="@+id/textView2" android:layout_gravity="left|center_vertical"/>
    <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/name" android:layout_gravity="left|center_vertical"
            android:hint="Name"/>

    <Space
        android:layout_width="fill_parent"
        android:layout_height="20px"
        android:id="@+id/space"/>

    <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Done"
            android:id="@+id/done" android:layout_gravity="center"/>
</LinearLayout>

对于登录
活动
只需使用所需的
视图(
微调器
文本视图
按钮
,等等)所需的
布局

如果问题屏幕都是相同的,那么如果需要,您可以使用
片段
查看页面
。这应该可以很好地满足您的需要。这将允许您为每个问题提供新数据,但很容易保持相同的
布局

我没怎么看过,但他的教程通常都很好

如果
ViewPager
超出了您当前的需要/需要,您只需为问题设置一个
View
,例如
TextView
,并在单击
按钮时更改文本

编辑

更改您的
意图
以使用该
上下文

  Intent i = new Intent(activity, Quiz.class);
  activity.startActivity(i);

您可以通过两个不同的活动来实现这一点,每个活动都有不同的内容视图(XML文件)。 默认启动程序活动将是带有登录布局文件的登录屏幕

第二个活动应该是有一个视图来处理问题。这可以通过使用您的apt视图组来完成,如RelativeLAyout、LinearLayout、FrameLayout等。互联网上每个视图都有大量示例。只需搜索每个视图的教程即可


最终视图(已完成)可以在同一活动中使用不同的视图或仅使用
AlertDialog

来实现,因此如果我使用
查看页面
,我应该生成每个
视图
,并将它们全部放在
数组
中,并将该
数组
与VewPager一起使用?不,将数据放在
数组
中,然后可以使用相同的
视图
s并只更新每个页面中的数据似乎更容易拥有一个
视图
并用新文本更新视图。我如何简单地将活动视图设置为
quick.xml
?我可以处理按钮侦听器和文本更改等。您不希望在同一页面中更改xml文件
活动
。这只会导致更多的混乱和错误。我想说的是,如果您只是在同一个
文本视图
中显示不同的问题,那么您可以将问题存储在
数组列表
哈希映射
,或者任何您想要的内容中,然后更改该
文本视图
。我需要d有一个连接屏幕、测验屏幕和完成屏幕。我创建了连接屏幕,我创建了根据当前问题更改文本的测验屏幕。我将创建完成屏幕。我需要知道的是如何将活动屏幕从连接切换到测验。此外,我相信我在自己的活动类中有测验屏幕。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_gravity="center">

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Question"
            android:id="@+id/textView"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New RadioButton"
            android:id="@+id/radioButton"
            android:layout_gravity="center"/>

        <RadioButton
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="New RadioButton"
            android:id="@+id/radioButton1"/>

        <RadioButton
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="New RadioButton"
            android:id="@+id/radioButton2"/>

        <RadioButton
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="New RadioButton"
            android:id="@+id/radioButton3"/>

    </RadioGroup>
</LinearLayout>
package me.nrubin29.quiz.student;

import android.app.Activity;
import android.content.Intent;
import android.widget.Toast;
import me.nrubin29.quiz.student.receivedQuiz.Quiz;

import java.io.EOFException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class Connection {

    private Connection() { }

    private static Connection instance = new Connection();

    public static Connection getInstance() {
        return instance;
    }

    private Quiz quiz;
    private Socket socket;
    private Thread reader;
    private ObjectInputStream inputStream;
    private ObjectOutputStream outputStream;

    public void initConnection(final Activity activity, final String ip, final String port, final String name) {
        new Thread(new Runnable() {
            public void run() {
                try {
                    activity.runOnUiThread(new Runnable() {
                        public void run() {
                            Toast.makeText(activity.getApplicationContext(), "Starting connection to " + ip + ":" + Integer.parseInt(port), Toast.LENGTH_SHORT).show();
                        }
                    });

                    socket = new Socket(ip, Integer.parseInt(port));

                    activity.runOnUiThread(new Runnable() {
                        public void run() {
                            Toast.makeText(activity.getApplicationContext(), "Connected!", Toast.LENGTH_SHORT).show();
                        }
                    });

                    outputStream = new ObjectOutputStream(socket.getOutputStream());

                    inputStream = new ObjectInputStream(socket.getInputStream());

                    outputStream.writeObject(name);

                    quiz = new Quiz((String) inputStream.readObject());

                    Intent i = new Intent(activity, me.nrubin29.quiz.student.Quiz.class);
                    activity.startActivity(i);

                    reader = new Thread(new Runnable() {
                        public void run() {
                            while (true) {
                                try {
                                    Object in = inputStream.readObject();
                                }
                                catch (final EOFException e) { activity.runOnUiThread(new Runnable() { public void run() { Toast.makeText(activity.getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show(); } }); }
                                catch (Exception e) { e.printStackTrace(); }
                            }
                        }
                    });

                    reader.start();
                }
                catch (Exception e) { e.printStackTrace(); }
            }
        }).start();
    }

    public Quiz getQuiz() {
        return this.quiz;
    }
}

As far as the Activity for Quiz, I guess I should make a constructor that takes the question and answers and sets the text of the radio buttons? Also, how exactly (code example would rock) would I create go about answering the question with the code above?
  Intent i = new Intent(activity, Quiz.class);
  activity.startActivity(i);