【问题标题】:How to fix setError message appear in different languages?如何修复 setError 消息以不同的语言出现?
【发布时间】:2020-02-11 20:51:56
【问题描述】:

我创建了一个登录页面,该页面支持 2 种不同的语言(英语和法语)

当我从一种语言翻译成另一种语言时,该页面工作正常。

但问题是当我切换到 法语 时,setError 消息 不再出现用于字段验证。

此代码将根据语言选择加载页面:

 public boolean onOptionsItemSelected(MenuItem item) {

            switch (item.getItemId()) {

                case R.id.eng:
                String languageToLoad = "en";
                Locale locale = new Locale(languageToLoad);
                Locale.setDefault(locale);
                Configuration config = new Configuration();
                config.locale = locale;
                getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
                this.setContentView(R.layout.activity_main);
                break;

                case R.id.fr:
                languageToLoad = "fr";
                locale = new Locale(languageToLoad);
                Locale.setDefault(locale);
                config = new Configuration();
                config.locale = locale;
                getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
                this.setContentView(R.layout.activity_main);
                break;

                default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

这是对用户名字段和setError消息的验证:

private boolean validateUsername(){

        String username = usernameEditText.getText().toString().trim();

        if(username.isEmpty()){

            usernameEditText.setError("Field can't be empty");
            return false;

        } else if(username.length() > 15){

            usernameEditText.setError("Username too long");
            return false;

        } else {
            usernameEditText.setError(null);
            return true;
        }
    }

XML 代码:

      <?xml version="1.0" encoding="utf-8"?>
<android.widget.RelativeLayout 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"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/usernameEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="300dp"
        android:ems="10"
        android:hint="@string/YourUsername"
        android:inputType="textPersonName"
        android:maxLength="15"
        app:errorEnabled="true" />

    <Button
        android:id="@+id/signupButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/test3"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:onClick="signupLogin"
        android:hint="@string/login" />
</android.widget.RelativeLayout>

两种语言之间的弹出消息:

这里我用 English 设置错误信息,但是有什么方法可以添加到 setError 信息以支持不同的语言?

谢谢!

【问题讨论】:

    标签: java android android-resources android-textinputlayout material-components


    【解决方案1】:

    使用来自字符串资源的setError 消息并使用本地化字符串作为首选语言和英语

    【讨论】:

      【解决方案2】:

      使用类似的东西:

      <com.google.android.material.textfield.TextInputLayout
          android:id="@+id/username"
          ..>
      
      TextInputLayout username = findViewById(R.id.username);
      username.setError(getResources().getString(R.string.xxxx));
      

      然后在res/values/strings.xmlres/values-fr/strings.xml中定义字符串

      【讨论】:

      • 你好@Gabriele Mariotti 我也尝试了你的步骤,但是当我切换语言时没有任何反应,当我点击登录按钮时弹出消息没有出现。
      【解决方案3】:

      为此,您可以本地化您的应用。参考这个链接Localize your app

      转到 res -> -> 右键单击 -> 新建 -> 值资源文件 -> 选择区域设置 -> 添加法语 -> 确定

      string.xml

      <string name="empty_error">Field can't be empty</string>
      <string name="character_exceed_error">Username too long </string>
      

      string.xml(fr)

      <string name="empty_error">Le champ ne peut pas être vide</string>
      <string name="character_exceed_error">Nom d'utilisateur trop long </string>
      

      你只需要从资源中获取字符串值,如下所示

      usernameEditText.setError(getString(R.string.empty_error);
      

      【讨论】:

      • 你好@Rajnish suryavanshi!我按照您的步骤操作,并将代码更新为 usernameEditText.setError(getResources().getString(R.string.empty_error));但在法语语言中,当我点击登录按钮时,错误保持不变,没有任何反应,弹出消息不会出现。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-14
      • 1970-01-01
      • 2014-01-14
      • 2017-03-09
      • 2021-10-06
      • 2021-04-16
      • 1970-01-01
      相关资源
      最近更新 更多