简体   繁体   中英

Android: Button Click || New Activity? || NEED Assistance

I am looking to essentially make program that will do the following and am having trouble figuring out buttons and transitioning activities with them. No input other than button presses needed.

1-Textview containing a question. 2-Button to display the answer to question. 3-Textview to put to answer provided by button click. 4-Button to display next activity formatted the same way (5 times repeated) 5-Also need buttons to end the application all together.

I know this is probably one of the lowest level questions, but I cannot find how to get the buttons to do anything (Part 2,4,and 5 of the list above)

I assume it has something to do with initially setting the answer to the question invisible until button is pressed then revealing it.

Please help me with this elementary level question! :(

Below is my layout file, and the main java file is pretty stingy for this project so it will not be attached in its current state as it is mostly a layout issue I am trying to resolve.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<Textview android:id="@+id/Questions"
    android:layout_weight="1"
    android:layout_width="wrap_content"
    android:layout_height="0dip"
    android:text="@string/Q1"   />

<Button android:id="@+id/QButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_question" 
android:onClick="buttonQuestion" />

<Button android:id="@+id/AButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send" 
    android:onClick="buttonAnswer"/>

<Textview android:id="@+id/Answers"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:hint="@string/edit_message"
android:onClick="sendMessage" />

<Button android:id="@+id/QuitButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_quit" 
    android:onClick="buttonQuit"/>

You can set text of your textview using findViewById (to find specific textview ) and setText() (to set your answer). Refer to these links: findViewByID and setText . Initially your textView text referring to answer could be set to something like empty string setText("");

Button answerButton = (Button) findViewById(R.id.AButton);
    answerButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            TextView answerText = (TextView) findViewById(R.id.Answers);
            answerText.setText("Your answer");
        }
    });

Also you can keep your Q&A in string arrays (if you already have couple questions and answers) and just iterate trough arrays on button click (your next question button. I think it's QButton in your layout) without the need for new activity.

All you need to do is create methods for each button in the java class where you are using this layout like so.

public void buttonQuestion(View v){
        TextView answerText = (TextView) findViewById(R.id.Answers);
        answerText.setText("Your answer");
    }

public void buttonAnswer(View v){
            //do something
        }

public void sendMessage(View v){
            //do something
        }

public void buttonQuit(View v){
            finish();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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