单击按钮时将 EditText 文本转换为大写和小写?
Convert EditText text to uppercase and lowercase , when clicked on button?
我想做的是从用户那里获取输入,然后在按钮单击事件上我想在 TextView
上显示 EditText
输入。在按钮点击侦听器上,textView 应该以全部大写显示输入字符串,然后单击同一个按钮应该将该字符串转换为小写并在 TextView
上显示。我怎样才能做到这一点?
这是我试过的。
var userInput = findViewById<EditText>(R.id.editText)
var caseButton = findViewById<Button>(R.id.upperLowerButton)
var caseView = findViewById<TextView>(R.id.textUpperLower)
caseButton.setOnClickListener {
caseView.text = userInput.text
}
您可以使用 .toUpperCase()
和 .toLowerCase()
作为您的字符串值,例如
userInput.text.toString().toUpperCase()
和
userInput.text.toString().toLowerCase()
您可以使用类似的东西:
val uppercase = userInput.text.toString().toUpperCase()
val lowerCase = uppercase.toLowerCase()
您可以像下面这样尝试。
var isUpperCase = false;
var txt = userInput.text.toString()
caseButton.setOnClickListener {
if(isUpperCase){
txt = txt.toLowerCase()
isUpperCase = false
}else{
txt = txt.toUpperCase()
isUpperCase = true
}
caseView.text = txt
}
使用方法 - toUpperCase() 和 toLowerCase() 可以轻松解决这个问题。
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String h = textView.getText().toString();
if (isCaps) {
textView.setText(h.toUpperCase());
isCaps = false;
}
else {
textView.setText(h.toLowerCase());
isCaps = true;}
}
});
在 Kotlin 中,由于 toUperCase()
和 toLowerCase()
已弃用,我们可以使用 uppercase()
代替 toUpperCase()
,使用 lowercase()
代替 toLowerCase()
。
例如,
val lower="abc"
Log.e("TAG", "Uppercase: ${lower.uppercase()}" ) //ABC
val upper="ABC"
Log.e("TAG", "Lowercase: ${upper.lowercase()}" ) //abc
我想做的是从用户那里获取输入,然后在按钮单击事件上我想在 TextView
上显示 EditText
输入。在按钮点击侦听器上,textView 应该以全部大写显示输入字符串,然后单击同一个按钮应该将该字符串转换为小写并在 TextView
上显示。我怎样才能做到这一点?
这是我试过的。
var userInput = findViewById<EditText>(R.id.editText)
var caseButton = findViewById<Button>(R.id.upperLowerButton)
var caseView = findViewById<TextView>(R.id.textUpperLower)
caseButton.setOnClickListener {
caseView.text = userInput.text
}
您可以使用 .toUpperCase()
和 .toLowerCase()
作为您的字符串值,例如
userInput.text.toString().toUpperCase()
和
userInput.text.toString().toLowerCase()
您可以使用类似的东西:
val uppercase = userInput.text.toString().toUpperCase()
val lowerCase = uppercase.toLowerCase()
您可以像下面这样尝试。
var isUpperCase = false;
var txt = userInput.text.toString()
caseButton.setOnClickListener {
if(isUpperCase){
txt = txt.toLowerCase()
isUpperCase = false
}else{
txt = txt.toUpperCase()
isUpperCase = true
}
caseView.text = txt
}
使用方法 - toUpperCase() 和 toLowerCase() 可以轻松解决这个问题。
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String h = textView.getText().toString();
if (isCaps) {
textView.setText(h.toUpperCase());
isCaps = false;
}
else {
textView.setText(h.toLowerCase());
isCaps = true;}
}
});
在 Kotlin 中,由于 toUperCase()
和 toLowerCase()
已弃用,我们可以使用 uppercase()
代替 toUpperCase()
,使用 lowercase()
代替 toLowerCase()
。
例如,
val lower="abc"
Log.e("TAG", "Uppercase: ${lower.uppercase()}" ) //ABC
val upper="ABC"
Log.e("TAG", "Lowercase: ${upper.lowercase()}" ) //abc