如何在 Android textview 中使用 toLowerCase()?
此示例演示如何在 Android textview 中使用 toLowerCase()。
步骤 1 − 在 Android Studio 中新建一个项目,依次转到文件 ⇒ 新建项目,然后填写所有必需的详细信息以新建一个项目。
步骤 2 − 将以下代码添加到 res/layout/activity_main.xml 中。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity" />
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:hint="Enter name"
android:layout_height="wrap_content" />
<Button
android:id="@+id/click"
android:text="Click"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:textSize="25sp"
android:layout_height="wrap_content" />
</LinearLayout>在上述代码中,我们已将名称作为编辑文本,当用户点击按钮时,它将获取数据并将其转换成小写字母。
步骤 3 − 将以下代码添加到 src/MainActivity.java 中
package com.example.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText name;
Button button;
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = findViewById(R.id.name);
button = findViewById(R.id.click);
text = findViewById(R.id.textview);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!name.getText().toString().isEmpty()) {
if (name.getText().toString().length() > 0) {
String tolowercase = name.getText().toString().toLowerCase();
text.setText(String.valueOf(tolowercase));
}
} else {
name.setError("Plz enter name");
}
}
});
}
}试试运行你的应用程序。我假设你已将你的实际 Android 移动设备连接到电脑。要从 Android Studio 运行应用,请打开项目的某个活动文件,然后点击工具栏中的运行
图标。选择你的移动设备作为选项,然后查看你的移动设备,它将显示你的默认屏幕 &−

在上述结果中,输入字符串“KRISHNA”(全大写),它将返回小写字母 krishna。
点击此处下载项目代码
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP