0

これは、これまでで最も難しい質問の 1 つですが、私の Android アプリでは、いくつかの異なる「画面」が必要です。ログインする場所と、質問を表すビュー(複数の質問があるため、質問ごとにビューを作成し、次のボタンがクリックされたときに次のビューに変更する必要があります.フローチャート:

ユーザーがアプリを開く -> ログイン画面 -> ユーザーがログインする -> 質問 1 の自動生成ビューへの変更を表示する -> [次へ] ボタンをクリックする -> 質問 2 -> すべての質問を行う -> 完了したことを示す最終ビュー。

2 つのレイアウト ファイルを作成しました。1xmlつは main と呼ばれ、もう 1 つは quiz (質問を表します) と呼ばれます。

必要なことは何でも詳しく説明します。

アップデート

ここに私の2つの活動クラスがあります:

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);
            }
        });
    }
}

package me.nrubin29.quiz.student;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;

public class Quiz extends Activity {

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

        final RadioButton radioButton1 = (RadioButton) findViewById(R.id.radioButton);
        final RadioButton radioButton2 = (RadioButton) findViewById(R.id.radioButton1);
        final RadioButton radioButton3 = (RadioButton) findViewById(R.id.radioButton2);
        final RadioButton radioButton4 = (RadioButton) findViewById(R.id.radioButton3);

        radioButton1.setText("Testing!");
    }
}

そして、ここに私の2つの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>

<?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>

アップデート!そして、これが私の新しい Connection クラスです。

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?
4

2 に答える 2

0

ログインには、必要なもの( 、、など) を使用して、必要なActivityものを使用してください。layoutViewSpinnerTextViewButton

質問画面がすべて同じになる場合は、必要に応じてFragments組み合わせて使用​​できViewPagerます。これは、ニーズに合わせてうまく機能するはずです。これにより、質問ごとに新しいデータを提供できますが、同じデータを簡単に保持できますlayout

VeiwPager ドキュメント

ViewPager の例私はあまり見ていませんが、彼のチュートリアルはたいていかなり良いものです。

ViewPager今必要以上に必要な場合はView、a などの質問用の1 つを用意し、をクリックしTextViewたときにテキストを変更することができます。Button

編集

Intentそれを使用するように変更しますContext

  Intent i = new Intent(activity, Quiz.class);
  activity.startActivity(i);
于 2013-09-23T22:58:10.303 に答える
0

これを実装するには、2 つの異なるアクティビティを用意し、それぞれに異なるコンテンツ ビュー (XML ファイル) を設定します。デフォルトの Launcher アクティビティは、ログイン レイアウト ファイルを含むログイン画面になります。

2 番目のアクティビティは、質問を処理するためのビューを持つ必要があります。これは、RelativeLAyout、LinearLayout、FrameLayout などの適切なビュー グループを使用して実行できます。インターネットには、それぞれに多数の例があります。それぞれのチュートリアルを検索してください。

最終的なビュー (これで完了です) は、別のビューまたはAlertDialog.

于 2013-09-23T22:58:53.573 に答える