嗨我需要一些帮助来创建一个函数,可以用第一个字母和另一个EditText
的第一个元音填充EditText
。
这样的事情:
EditText = Jhon
输出= JO
我有这个但仍然没有工作;
public class MainActivity extends Activity {
private EditText Result;
private EditText MyName;
private EditText showResult;
private String MyVowel;
private String cadena, PartOne;
private int count;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Result = (TextView) findViewById(R.id.showResult);
MyName= (EditText)findViewById(R.id.NameInput);
getInfo = (Button)findViewById(R.id.getInfoButton);
getInfo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String inputText = myName.getText().toString();
String partOne= inputText.substring(0,1);
for(int i=0;i <inputText.length();i++){
if((inputText.charAt(i) == 'a') ||
(inputText.charAt(i) == 'e') ||
(inputText.charAt(i) == 'i') ||
(inputText.charAt(i) == 'o') ||
(inputText.charAt(i) == 'u')) {
System.out.println(cadena);
}
}
showResult.setText(PartOne);
}
});
}
}
答案 0 :(得分:1)
你没有说过你的问题。如果您遇到逻辑,请参阅以下代码,
private String getShortForm(String inputText){
List<Character> vowels = new ArrayList<Character>() {
{
add('a');
add('e');
add('i');
add('o');
add('u');
add('A');
add('E');
add('I');
add('O');
add('U');
}
};
String partOne = inputText.substring(0, 1);
for (int i = 0; i < inputText.length(); i++) {
char c = inputText.charAt(i);
if(vowels.contains(c)){
partOne += c;
break;
}
}
return partOne.toUpperCase();
}
onClick
内的用法,
Result.setText(getShortForm(inputText));
答案 1 :(得分:0)
这是解决方案
public class MainActivity extends AppCompatActivity{
private TextView Result;
private EditText MyName;
private Button getInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Result = (TextView) findViewById(R.id.showResult);
MyName= (EditText)findViewById(txtText);
getInfo = (Button)findViewById(btnSpeak);
getInfo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String inputText = MyName.getText().toString();
String partOne= inputText.substring(0,1);
Log.e("inputText: "+inputText, "part one: "+partOne);
String vowels = "";
for(int i=0;i <inputText.length();i++){
if((inputText.charAt(i) == 'a') ||
(inputText.charAt(i) == 'e') ||
(inputText.charAt(i) == 'i') ||
(inputText.charAt(i) == 'o') ||
(inputText.charAt(i) == 'u') ||
(inputText.charAt(i) == 'A') ||
(inputText.charAt(i) == 'E') ||
(inputText.charAt(i) == 'I') ||
(inputText.charAt(i) == 'O') ||
(inputText.charAt(i) == 'U')){
vowels = vowels+inputText.charAt(i);
}
}
Log.e("inputText: "+inputText, "vowelsFinal: "+vowels);
String partTwo = vowels.substring(0,1);
Log.e("inputText: "+inputText, "partTwo: "+partTwo);
String finalString = partOne+partTwo;
Log.e("inputText: "+inputText, "finalString: "+finalString);
Result.setText(finalString);
}
});
}
}
希望这能解决您的问题:)