1

我正在尝试编写一个简单的android 应用程序来熟悉sdk,但我不知道为什么setText 方法没有改变TextView。基本上我想要发生的是让用户输入一个字符串,让用户选择在字符串上运行的方法并将输出设置为 Textview,但我似乎无法设置 TextView。我尝试在 onCreate 中调用该方法,在案例之后的 switch 中以及在每种情况下都没有将文本设置为输出值。有谁知道我做错了什么?

package com.example.firstapp;

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

public class TextChange extends Activity implements OnClickListener{

    Button findUpperCase, everyOther, vowelReplace, vowelCount;
    EditText input;
    TextView output; 
    String inputText, outputText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.textchanger);
        initializeVar();
        output.setText(outputText);
    }

    public void initializeVar(){
        findUpperCase = (Button) findViewById(R.id.finduppercase);
        everyOther = (Button) findViewById(R.id.everyother);
        vowelReplace = (Button) findViewById(R.id.vreplace);
        vowelCount = (Button) findViewById(R.id.vcount);
        input = (EditText) findViewById(R.id.stringinput);
        output = (TextView) findViewById(R.id.output);
    }

    public static String findUpperCase(String x){
        //Initialize output value
                String output = "";
        //For loop to loop through each character of the string
                for (int i = 0; i <= x.length()-1; i++)
                {
        //Convert letter to char value and compare it to the ASCII values for capital letters
                    if (Character.isUpperCase(x.charAt(i)))
                    {
        //Add each capital letter to the output string
                        output += x.substring(i, i+1) + " ";

                    }
                }   
        //If not uppercase letters tell user 
                if (output.equals("")){
                    output = "No upper case characters";
                }
        //Return the capital letters or message to user and exit method
                return output;

            }   
        //Method used to get every other letter     
            public static String everyOther(String x){
        //Set initial value of output
                String output = "";
        //Use for loop to get every other character by incrementing i by 2 each time
                for(int i=0; i<=x.length() - 1; i+=2){
        //Set letter equal to a temperary holder
                    char temp = x.charAt(i);
        //If it is the first letter add to output without a space preceeding it
                    if (i == 0){
        //New output is set as the old output plus the temp value without a space
                        output = output + temp; 
                    }
        //If it is not the first letter do this
                    else{
        //New output is set as the old output plus the temp value with a space
                    output = output + " " + temp;
                    }
                }
        //Return the output
                return output;

            }
        //Method to replace the vowel with "_"
            public static String vowelReplace(String x){
        //Search each string for vowels and ignore the case, when found replace with a "_"
                String output = x.replaceAll("(?i)[aeiou]","_");
        //Return the output
                return output;
            }
        //Method to count the vowels
            public static int vowelCount(String x){
        //Set a counter     
                int count = 0;
        //Set an array of char characters containing all the letters
                char vowels[] = {'a','e','i','o','u','A','E','I','O','U'};
        //For loop to check each letter for vowels
                for (int i = 0; i <= x.length() - 1; i++){
        //For loop to check each character against values in vowels array
                    for(int z = 0; z <= 9; z++){
        //If a vowel is found increment the count
                        if (x.charAt(i) == vowels[z]){
        //Increment the count
                            count++;
                        }
                    }
                }
        //Return the number of vowels
                return count;
            }
        //Method to identify the vowel positions
            public static String vowelPositions(String x){
        //Initialize an output variable
                    String output = "";
        //Set a temp string variable
                    String values = "";
        //Set an array of char characters containing all the letters
                    char vowels[] = {'a','e','i','o','u','A','E','I','O','U'};
        //For loop to check each letter for vowels
                        for(int z = 0; z < x.length(); z++){
        //For loop to check each character against values in vowels array                   
                            for (int i = 0; i <= 9; i++){
        //Check if each character is equal to any value in the array value
                                if (x.charAt(z) == vowels[i]){
        //If it is the first vowel found do not add a space before adding to output                     
                                    if(output.equals("")){
        //Set first value of output
                                        output = "[" + z + "]";
                                    }
                                    else{
        //Add each position of the vowels to the output string
                                        values = " [" + z + "]";
        //Set new value of outputs to old value plus the value of values
                                        output = output + values;
                                    }
                                }

                            }
                        }
        //Return the location of the vowels                         
                        return "Vowels are located at the following indexes:" + output;
                    }

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                inputText = input.getText().toString();
                switch(v.getId()){
                    case R.id.finduppercase:
                        outputText = findUpperCase(inputText);
                        break;
                    case R.id.everyother:
                        outputText = everyOther(inputText);
                        break;
                    case R.id.vcount:
                    outputText=Integer.toString(vowelCount(inputText));
                        break;
                    case R.id.vpos:
                        outputText = vowelPositions(inputText);
                        break;
                    case R.id.vreplace:
                        outputText = vowelReplace(inputText);
                        break;
                }


            }


}
4

2 回答 2

1

有谁知道我做错了什么?

onCreate()中,您正在调用setText()TextView但您传递的参数是null,因为您尚未初始化output

您的代码中没有其他地方调用setText().

于 2013-11-09T20:21:11.237 回答
1

首先,您没有setTextOnclick方法中调用任何地方,其次您没有将侦听器设置为按钮

1) 将 onClickListners 设置为您的按钮

 public void initializeVar(){
    findUpperCase = (Button) findViewById(R.id.finduppercase);
            everyOther = (Button) findViewById(R.id.everyother);
            vowelReplace = (Button) findViewById(R.id.vreplace);
            vowelCount = (Button) findViewById(R.id.vcount);
            findUpperCase.setOnClickListener(this);
            everyOther.setOnClickListener(this);
            vowelReplace.setOnClickListener(this);
            vowelCount.setOnClickListener(this);
    }

2)在onClick方法中设置文本:

 @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                inputText = input.getText().toString();
                switch(v.getId()){
                    case R.id.finduppercase:
                        outputText = findUpperCase(inputText);
                        output.setText(outputText);
                        break;
                    case R.id.everyother:
                        outputText = everyOther(inputText);
                        output.setText(outputText);
                        break;
                    case R.id.vcount:
                    outputText=Integer.toString(vowelCount(inputText));
                    output.setText(outputText);
                        break;
                    case R.id.vpos:
                        outputText = vowelPositions(inputText);
                        output.setText(outputText);
                        break;
                    case R.id.vreplace:
                        outputText = vowelReplace(inputText);
                        output.setText(outputText);
                        break;
                }


        }
于 2013-11-09T20:26:52.943 回答