【问题标题】:EditText always showing nullEditText 总是显示为空
【发布时间】:2013-09-05 00:28:48
【问题描述】:

我的 xml

<TextView 
        android:id="@+id/newPlaceLatLable"
        android:text="Place Latitude"
        android:layout_width="150dp"
        android:layout_height="40dp"
       android:layout_below="@id/newPlaceLable"/>
    <EditText
        android:id="@+id/newPlaceLat"
        android:layout_width="150dp"
        android:layout_height="40dp"
        android:layout_toRightOf="@id/newPlaceLatLable" 
         android:layout_below="@id/newPlaceLable"
         android:inputType="numberDecimal"
        />
    <TextView 
        android:id="@+id/newPlaceLonLable"
        android:text="Place Longitude"
        android:layout_width="150dp"
        android:layout_height="40dp"
       android:layout_below="@id/newPlaceLatLable"/>
    <EditText
        android:id="@+id/newPlaceLon"
        android:layout_width="150dp"
        android:layout_height="40dp"
        android:layout_toRightOf="@id/newPlaceLonLable" 
        android:layout_below="@id/newPlaceLatLable"
        android:inputType="numberDecimal"
        />

在代码中

if (et1.getText().toString().matches(""))
            Toast.makeText(getApplicationContext(), "Name can't be null", Toast.LENGTH_SHORT).show();
        else if (et2.getText().toString().matches(""))
            Toast.makeText(getApplicationContext(), "Latitute can't be null", Toast.LENGTH_SHORT).show();
        else  if (et3.getText().toString().matches(""))
            Toast.makeText(getApplicationContext(), "Longitute can't be null", Toast.LENGTH_SHORT).show();

et1、et2 和 et3 在哪里

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change);
        et1 = (EditText) findViewById(R.id.newPlaceText);        
         et2 = (EditText) findViewById(R.id.newPlaceLat);
         et3 = (EditText) findViewById(R.id.newPlaceLon);

但每次我运行应用程序时,它都会吐司“Latitute 不能为空”。虽然 et2 里面有文字,但是这里没有显示。请看一下。有什么问题

【问题讨论】:

  • 那是你完整的 xml 代码。这里只显示两个edittext。

标签: android xml android-edittext


【解决方案1】:

您可以按照 @blackbelt 的建议或这样的方式使用..

if (et1.getText().toString().equals(""))
            Toast.makeText(getApplicationContext(), "Name can't be null", Toast.LENGTH_SHORT).show();
else if (et2.getText().toString().equals(""))
            Toast.makeText(getApplicationContext(), "Latitute can't be null", Toast.LENGTH_SHORT).show();
else  if (et3.getText().toString().equals(""))
            Toast.makeText(getApplicationContext(), "Longitute can't be null", Toast.LENGTH_SHORT).show();

希望这会有所帮助...

【讨论】:

    【解决方案2】:

    您想检查一下EditText 是否填充了相同的值。

    matches("")
    

    不是你需要的。

    检查String的lenght是否>0,或者使用equals("")

    if (et1.getText().toString().length() <= 0)
                Toast.makeText(getApplicationContext(), "Name can't be null", Toast.LENGTH_SHORT).show();
    else if (et2.getText().toString().length() <= 0)
                Toast.makeText(getApplicationContext(), "Latitute can't be null", Toast.LENGTH_SHORT).show();
    else  if (et3.getText().toString().length() <= 0)
                Toast.makeText(getApplicationContext(), "Longitute can't be null", Toast.LENGTH_SHORT).show();
    

    【讨论】:

    • 还是不行。 Actullay et2.getText() 总是空白的,如果里面有文字。
    • 不能为空,同时有文字。
    • 我的意思是。当我在应用程序中输入文本时。这很奇怪。正如 et1 显示它有文本但 et2 没有。
    • 你的最后一句话对我来说没有意义
    • 我希望你没有在代码中的任何地方使用et2 = new EditText(context);,这会改变引用..猜一猜.....
    【解决方案3】:

    这样用

    if (et1.getText().toString().trim().length() <= 0)
                Toast.makeText(getApplicationContext(), "Name can't be null", Toast.LENGTH_SHORT).show();
    else if (et2.getText().toString().trim().length() <= 0)
                Toast.makeText(getApplicationContext(), "Latitute can't be null", Toast.LENGTH_SHORT).show();
    else  if (et3.getText().toString().trim().length() <= 0)
                Toast.makeText(getApplicationContext(), "Longitute can't be null", Toast.LENGTH_SHORT).show();
    

    【讨论】:

      【解决方案4】:

      matches() 和 equal() 的区别在于,matches() 会检查 String 与正则表达式模式的匹配,而 equal() 会将此 Comparator 与指定的 Object 进行比较并指示它们是否相等,请参阅此回答

      https://stackoverflow.com/a/9700152/2240535

      【讨论】:

        猜你喜欢
        • 2012-04-30
        • 1970-01-01
        • 2017-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-20
        • 1970-01-01
        • 2020-09-20
        相关资源
        最近更新 更多