Android Studie App Development接收字符串输入或Settting Image资源

时间:2016-05-20 02:15:32

标签: java android import

好的,所以我在Android工作室中为Android App个性测验编写了这段代码。我的GUi窗口工作正常,它正确链接到我的手机,看起来不错。但是,当我单击提交按钮(上传到强制)时,当我输入正确的响应时,不会显示不同的图片。我知道这不是我的照片的问题,因为如果我把ImageSetter从开关盒中取出它会出现在App中。所以我得出的结论是,不知何故,我在GUI窗口中从EditText字段框中读取文本的代码不对,或者我正在构建字母列表以检查错误。 (是的,我知道把它放在一个arraylist只是为了把它变成一个字符串是多余的,但我是按照我想到的第一种方式做的。有任何建议我花了几个小时在这上面,不能想到出了什么问题?你们是我最欣赏它。

`

import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    EditText nameTxt, answer2, answer3, answer4, answer5, answer6, answer7, answer8;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

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

        final ImageView imageview = (ImageView)findViewById(R.id.showCharacter);


        //have to cast it into type (EditText) since by default is is a GUI container and an error
        //will be generated if you do not again specify that the container is type EditText
        nameTxt = (EditText) findViewById(R.id.inputA1);//links nameTxt to GUI editText field
        answer2 = (EditText) findViewById(R.id.inputA2);//links nameTxt to GUI editText field
        answer3 = (EditText) findViewById(R.id.inputA3);//links nameTxt to GUI editText field
        answer4 = (EditText) findViewById(R.id.inputA4);//links nameTxt to GUI editText field
        answer5 = (EditText) findViewById(R.id.inputA5);//links nameTxt to GUI editText field
        answer6 = (EditText) findViewById(R.id.inputA6);//links nameTxt to GUI editText field
        answer7 = (EditText) findViewById(R.id.inputA7);//links nameTxt to GUI editText field
        answer8 = (EditText) findViewById(R.id.inputA8);//links nameTxt to GUI editText field


        ArrayList<String> responses = new ArrayList<String>();//string array to hold temp responses

        String respStr = "";//used for comparison to the answer key since it is easier than using an
        //arraylist for comparison.

        String character = "";// honestly this doesnt output anything or do anything except help me
        //keep track of which key corresponds to which picture so I dont get confused

        //creates a new button  called addBtn and likes it to the GUI button btnAdd
        final Button addBtn = (Button) findViewById(R.id.btnAdd);
        if (addBtn != null) {//designed to not allow the submission button to be clickable unless
            //the first field is filled out
            addBtn.setOnClickListener(new View.OnClickListener() {
             @Override
              public void onClick(View view) {
               Toast.makeText(getApplicationContext(),"Using the Force", Toast.LENGTH_SHORT).show();
              }
            }) ;
        }

        nameTxt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int start, int       before, int count) {
                addBtn.setEnabled(String.valueOf(nameTxt.getText()).trim().length() > 0);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        //takes the input of the first question using getText() then it must convert getText which
        //of type EditText to type string using toString. Then the variable is set to itself with
        //no leading or trailing spaces using the .trim(0 and then made lowercase by using the
        //toLowerCase. equalsIgnoreCase is used as a redundant wway to ensure that input from the
        //user can be either upper or lower case.
        String q1S = "";
        q1S = nameTxt.getText().toString();
        q1S = q1S.trim().toLowerCase();
        if (q1S.equalsIgnoreCase("a")){
            responses.add("a");
        }
        if (q1S.equalsIgnoreCase("b")){
            responses.add("b");
        }
        if (q1S.equalsIgnoreCase("c")){
            responses.add("c");
        }
        if (q1S.equalsIgnoreCase("d")){
            responses.add("d");
        }
        if (q1S.equalsIgnoreCase("e")){
            responses.add("e");
        }
        for(int i =0; 0>responses.size(); i++){

            respStr += responses.get(i);
        }


        //takes the input of the second question using getText() then it must convert getText which
        //of type EditText to type string using toString. Then the variable is set to itself with
        //no leading or trailing spaces using the .trim(0 and then made lowercase by using the
        //toLowerCase. equalsIgnoreCase is used as a redundant wway to ensure that input from the
        //user can be either upper or lower case.
        String q2S = "";
        q2S = nameTxt.getText().toString();
        q2S = q2S.trim().toLowerCase();
        if (q2S.equalsIgnoreCase("a")){
            responses.add("a");
        }
        if (q2S.equalsIgnoreCase("b")){
            responses.add("b");
        }
        if (q2S.equalsIgnoreCase("c")){
            responses.add("c");
        }
        if (q2S.equalsIgnoreCase("d")){
            responses.add("d");
        }
        if (q2S.equalsIgnoreCase("e")){
            responses.add("e");
        }

        //takes the input of the third question using getText() then it must convert getText which
        //of type EditText to type string using toString. Then the variable is set to itself with
        //no leading or trailing spaces using the .trim(0 and then made lowercase by using the
        //toLowerCase. equalsIgnoreCase is used as a redundant way to ensure that input from the
        //user can be either upper or lower case.
        String q3S = "";
        q3S = nameTxt.getText().toString();
        q3S = q3S.trim().toLowerCase();
        if (q3S.equalsIgnoreCase("a")){
            responses.add("a");
        }
        if (q3S.equalsIgnoreCase("b")){
            responses.add("b");
        }
        if (q3S.equalsIgnoreCase("c")){
            responses.add("c");
        }
        if (q3S.equalsIgnoreCase("d")){
            responses.add("d");
        }
        if (q3S.equalsIgnoreCase("e")){
            responses.add("e");
        }


        //takes the input of the fourth question using getText() then it must convert getText which
        //of type EditText to type string using toString. Then the variable is set to itself with
        //no leading or trailing spaces using the .trim(0 and then made lowercase by using the
        //toLowerCase. equalsIgnoreCase is used as a redundant way to ensure that input from the
        //user can be either upper or lower case.
        String q4S = "";
        q4S = nameTxt.getText().toString();
        q4S = q4S.trim().toLowerCase();
        if (q4S.equalsIgnoreCase("a")){
            responses.add("a");
        }
        if (q4S.equalsIgnoreCase("b")){
            responses.add("b");
        }
        if (q4S.equalsIgnoreCase("c")){
            responses.add("c");
        }
        if (q4S.equalsIgnoreCase("d")){
            responses.add("d");
        }
        if (q4S.equalsIgnoreCase("e")){
            responses.add("e");
        }

        //takes the input of the fifth question using getText() then it must convert getText which
        //of type EditText to type string using toString. Then the variable is set to itself with
        //no leading or trailing spaces using the .trim(0 and then made lowercase by using the
        //toLowerCase. equalsIgnoreCase is used as a redundant way to ensure that input from the
        //user can be either upper or lower case.
        String q5S = "";
        q5S = nameTxt.getText().toString();
        q5S = q5S.trim().toLowerCase();
        if (q5S.equalsIgnoreCase("a")){
            responses.add("a");
        }
        if (q5S.equalsIgnoreCase("b")){
            responses.add("b");
        }
        if (q5S.equalsIgnoreCase("c")){
            responses.add("c");
        }
        if (q5S.equalsIgnoreCase("d")){
            responses.add("d");
        }
        if (q5S.equalsIgnoreCase("e")){
            responses.add("e");
        }


        //takes the input of the sixth question using getText() then it must convert getText which
        //of type EditText to type string using toString. Then the variable is set to itself with
        //no leading or trailing spaces using the .trim(0 and then made lowercase by using the
        //toLowerCase. equalsIgnoreCase is used as a redundant way to ensure that input from the
        //user can be either upper or lower case.
        String q6S = "";
        q6S = nameTxt.getText().toString();
        q6S = q6S.trim().toLowerCase();
        if (q6S.equalsIgnoreCase("a")){
            responses.add("a");
        }
        if (q6S.equalsIgnoreCase("b")){
            responses.add("b");
        }
        if (q6S.equalsIgnoreCase("c")){
            responses.add("c");
        }
        if (q6S.equalsIgnoreCase("d")){
            responses.add("d");
        }
        if (q6S.equalsIgnoreCase("e")){
            responses.add("e");
        }

        //takes the input of the seventh question using getText() then it must convert getText which
        //of type EditText to type string using toString. Then the variable is set to itself with
        //no leading or trailing spaces using the .trim(0 and then made lowercase by using the
        //toLowerCase. equalsIgnoreCase is used as a redundant way to ensure that input from the
        //user can be either upper or lower case.
        String q7S = "";
        q7S = nameTxt.getText().toString();
        q7S = q7S.trim().toLowerCase();
        if (q7S.equalsIgnoreCase("a")){
            responses.add("a");
        }
        if (q7S.equalsIgnoreCase("b")){
            responses.add("b");
        }
        if (q7S.equalsIgnoreCase("c")){
            responses.add("c");
        }
        if (q7S.equalsIgnoreCase("d")){
            responses.add("d");
        }
        if (q7S.equalsIgnoreCase("e")){
            responses.add("e");
        }

        //takes the input of the eigth question using getText() then it must convert getText which
        //of type EditText to type string using toString. Then the variable is set to itself with
        //no leading or trailing spaces using the .trim(0 and then made lowercase by using the
        //toLowerCase. equalsIgnoreCase is used as a redundant way to ensure that input from the
        //user can be either upper or lower case.
        String q8S = "";
        q8S = nameTxt.getText().toString();
        q8S = q8S.trim().toLowerCase();
        if (q8S.equalsIgnoreCase("a")){
            responses.add("a");
        }
        if (q8S.equalsIgnoreCase("b")){
            responses.add("b");
        }
        if (q8S.equalsIgnoreCase("c")){
            responses.add("c");
        }
        if (q8S.equalsIgnoreCase("d")){
            responses.add("d");
        }
        if (q8S.equalsIgnoreCase("e")){
            responses.add("e");
        }
        //imageview.setImageResource(R.drawable.sidious);




       switch(respStr){
            case "aabaacaa":
                character ="Sideous";
                imageview.setImageResource(R.drawable.sidious);
                break;
            case "abdbadaa":
                character ="Maul";
                imageview.setImageResource(R.drawable.maul);
                break;
            case "aadccdab":
                character ="Vader";
                imageview.setImageResource(R.drawable.vader);
                break;
            case "bbbdcaaa":
                character ="Boba Fett";
                imageview.setImageResource(R.drawable.boba);
                break;
            case "abeeadba":
                character ="Grevous";
                imageview.setImageResource(R.drawable.grievous);
                break;
            case "aaacbabb":
                character ="Windu";
                imageview.setImageResource(R.drawable.windu);
                break;
            case "abacbbbb":
                character ="Obi-Won-Kenobi";
                imageview.setImageResource(R.drawable.kenobi);
                break;
            case "bacbcaab":
                character ="Solo";
                imageview.setImageResource(R.drawable.solo);
                break;
            case "bbaccaba":
                character ="Clone";
                imageview.setImageResource(R.drawable.trooper);
                break;
            case "aacbbabb":
                character ="Anakin";
                imageview.setImageResource(R.drawable.anakin);
                break;
            default:
                character = "R2D2";
        }


        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

`

1 个答案:

答案 0 :(得分:0)

你的for循环有错误的条件。

您的代码:

for(int i =0; 0>responses.size(); i++){
    respStr += responses.get(i);
}

应该是:

for(int i=0; i<responses.size(); i++){
    respStr += responses.get(i);
}