如何在 Android 中限制 EditText 文本长度?
本示例演示如何在 Android 中限制 EditText 文本长度。
步骤 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: tools = "http://schemas.android.com/tools" android :layout_width= "match_parent" android :layout_height= "match_parent" android :layout_margin= "16dp" tools :context= ".MainActivity" > <EditText android :layout_width= "match_parent" android :layout_height= "wrap_content" android :hint= "Enter something..." android :maxLength= "10" /> </LinearLayout>
步骤 3 − 将以下代码添加到 src/MainActivity.java 中
package app.tutorialspoint.com.sample ;
import android.support.v7.app.AppCompatActivity ;
import android.os.Bundle ;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate (Bundle savedInstanceState) {
super .onCreate(savedInstanceState) ;
setContentView(R.layout. activity_main ) ;
}
}步骤 4 − 将以下代码添加到 androidManifest.xml 中
<? xml version= "1.0" encoding= "utf-8" ?> <manifest xmlns: android = "http://schemas.android.com/apk/res/android" package= "app.tutorialspoint.com.sample" > <application android :allowBackup= "true" android :icon= "@mipmap/ic_launcher" android :label= "@string/app_name" android :roundIcon= "@mipmap/ic_launcher_round" android :supportsRtl= "true" android :theme= "@style/AppTheme" > <activity android :name= ".MainActivity" > <intent-filter> <action android :name= "android.intent.action.MAIN" /> <category android :name= "android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
让我们尝试运行一下你的应用程序。我假设你已经将真实的 Android 移动设备与你的计算机连接起来了。要通过 Android Studio 运行该应用,打开项目的任意一个活动文件并单击工具栏上的 Run
图标。将你的移动设备选择为一个选项,然后查看移动设备,它会显示你的默认屏幕

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程语言
C++
C#
MongoDB
MySQL
Javascript
PHP