如果编辑文本为空,则禁用按钮。

huangapple go评论73阅读模式
英文:

form disable button if edit-Text is null(empty)

问题

我需要帮助,我创建了一个表单,通过按钮和几个编辑文本字段将用户文本数据发送到Firebase实时数据库,但我需要一些表单安全性,以防止用户只输入空数据。我尝试过,但无法使其工作,我确实需要帮助,比如禁用按钮或警告用户输入一些文本。
最好的问候。

XML:

<?xml version="1.0" encoding="utf-8"?>
<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">

    <TextView
        android:background="#F9B101"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="Buyers page"
        android:textAlignment="center"
        android:fontFamily="sans-serif-light"
        android:textSize="30sp"
        android:gravity="center_horizontal"
        android:textColor="#000"/>

    <EditText
        android:inputType="textPersonName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Name"
        android:id="@+id/name"
        android:layout_marginTop="100dp"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Address"
        android:id="@+id/address"
        android:layout_marginTop="150dp"/>

    <EditText
        android:inputType="phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="phone number"
        android:id="@+id/phoneNumber"
        android:layout_marginTop="200dp"/>

    <EditText
        android:inputType="textEmailAddress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="email address"
        android:id="@+id/email"
        android:layout_marginTop="250dp"/>

    <Button
        android:id="@+id/order"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="order"
        android:layout_marginBottom="10dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="#F9B101"
        android:textColor="#696969"
        android:textSize="20sp"
        android:textStyle="bold"/>

</RelativeLayout>

Java:

public class MainActivity extends AppCompatActivity {

    private EditText name, address, phoneNumber, email;
    private Button order;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        name = findViewById(R.id.name);
        address = findViewById(R.id.address);
        phoneNumber = findViewById(R.id.phoneNumber);
        email = findViewById(R.id.email);
        order = findViewById(R.id.order); //button

        order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("name", name.getText().toString());
                map.put("address", address.getText().toString());
                map.put("phoneNumber", phoneNumber.getText().toString());
                map.put("email", email.getText().toString());

                FirebaseDatabase.getInstance().getReference().child("Orders").push()
                        .setValue(map)
                        .addOnCompleteListener(new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {
                                Toast.makeText(MainActivity.this, "Order is processing...", Toast.LENGTH_SHORT).show();
                                startActivity(new Intent(MainActivity.this, SuccessActivity.class));
                            }
                        }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Toast.makeText(MainActivity.this, "Order Failed", Toast.LENGTH_LONG).show();
                    }
                });
            }
        });
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            hideSystemUI();
        }
    }

    private void hideSystemUI() {
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_IMMERSIVE
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN);
    }

    private void showSystemUI() {
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    }
}
英文:

i need help i made this form that sends user text data into Firebase-real-time database by a button and few edit-text but i need some form security so that users can't just enter blank data i have tried but can't get it to work i do need help just like a button disable or alert that tells the user to enter some text
best regards.

xml:


&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
tools:context=&quot;.MainActivity&quot;&gt;
&lt;TextView
android:background=&quot;#F9B101&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;50dp&quot;
android:text=&quot;Buyers page&quot;
android:textAlignment=&quot;center&quot;
android:fontFamily=&quot;sans-serif-light&quot;
android:textSize=&quot;30sp&quot;
android:gravity=&quot;center_horizontal&quot;
android:textColor=&quot;#000&quot;/&gt;
&lt;EditText
android:inputType=&quot;textPersonName&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:hint=&quot;Name&quot;
android:id=&quot;@+id/name&quot;
android:layout_marginTop=&quot;100dp&quot;
/&gt;
&lt;EditText
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:hint=&quot;Address&quot;
android:id=&quot;@+id/address&quot;
android:layout_marginTop=&quot;150dp&quot;
/&gt;
&lt;EditText
android:inputType=&quot;phone&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:hint=&quot;phone number&quot;
android:id=&quot;@+id/phoneNumber&quot;
android:layout_marginTop=&quot;200dp&quot;
/&gt;
&lt;EditText
android:inputType=&quot;textEmailAddress&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:hint=&quot;email address&quot;
android:id=&quot;@+id/email&quot;
android:layout_marginTop=&quot;250dp&quot;
/&gt;
&lt;Button
android:id=&quot;@+id/order&quot;
android:layout_width=&quot;100dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;order&quot;
android:layout_marginBottom=&quot;10dp&quot;
android:layout_alignParentBottom=&quot;true&quot;
android:layout_centerHorizontal=&quot;true&quot;
android:background=&quot;#F9B101&quot;
android:textColor=&quot;#696969&quot;
android:textSize=&quot;20sp&quot;
android:textStyle=&quot;bold&quot;
/&gt;
&lt;/RelativeLayout&gt;

java:


public class MainActivity extends AppCompatActivity {
private EditText name,address,phoneNumber,email;
private Button order;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = findViewById(R.id.name);
address = findViewById(R.id.address);
phoneNumber = findViewById(R.id.phoneNumber);
email = findViewById(R.id.email);
order = findViewById(R.id.order); //button
order.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
HashMap&lt;String,Object&gt; map = new HashMap&lt;&gt;();
map.put(&quot;name&quot;,name.getText().toString());
map.put(&quot;address&quot;,address.getText().toString());
map.put(&quot;phoneNumber&quot;,phoneNumber.getText().toString());
map.put(&quot;email&quot;,email.getText().toString());
FirebaseDatabase.getInstance().getReference().child(&quot;Orders&quot;).push()
.setValue(map)
.addOnCompleteListener(new OnCompleteListener&lt;Void&gt;() {
@Override
public void onComplete(@NonNull Task&lt;Void&gt; task) {
Toast.makeText(MainActivity.this, &quot;Order is processing...&quot;, Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this, SuccessActivity.class));
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this, &quot;Order Failed&quot;, Toast.LENGTH_LONG).show();
}
});
}
});
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
hideSystemUI();
}
}
private void hideSystemUI() {
// Enables regular immersive mode.
// For &quot;lean back&quot; mode, remove SYSTEM_UI_FLAG_IMMERSIVE.
// Or for &quot;sticky immersive,&quot; replace it with SYSTEM_UI_FLAG_IMMERSIVE_STICKY
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_IMMERSIVE
// Set the content to appear under the system bars so that the
// content doesn&#39;t resize when the system bars hide and show.
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
// Hide the nav bar and status bar
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN);
}
// Shows the system bars by removing all the flags
// except for the ones that make the content appear under the system bars.
private void showSystemUI() {
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
}
</details>
# 答案1
**得分**: 0
你可以通过检查以下方式来验证编辑文本数据是否为空:
```java
if (name.getText().toString() != "")

这样,你可以在 else 部分显示一个弹出窗口,指示字段为空。

英文:

You can check if the edit text data is empty or not by checking

if(name.getText().toString()!=&quot;&quot;)

In this way you can show a popup on else part saying that the fields are empty.

答案2

得分: 0

你可以检查文本字段是否为空,然后显示一个类似这样的提示:

if (name.getText().toString().equals("")) {
    Toast.makeText(getActivity(), "请填写您的姓名",
        Toast.LENGTH_LONG).show();
} else {
    // 发送姓名
}

如果需要的话,你也可以在姓名字段为空时禁用按钮,但我认为显示一个提示更好,这样用户就知道该怎么做。这更加用户友好。如果出于任何原因你想禁用按钮,你可以在你的if语句中使用以下代码:

yourButton.setEnabled(false); // yourButton是你按钮的名称!
英文:

You can check if the text field is empty, then just show a toast like this:

if(name.getText().toString().equals(&quot;&quot;){
Toast.makeText(getActivity(), &quot;Please Enter Your Name&quot;,
Toast.LENGTH_LONG).show();
}
else{
//Send the name
}

You can also disable the button if the name filed is empty, but I think it is better to show a toast so that the user knows what to do. It's just more userfriendly.
For any reason if you would like to disable the button you can use this code in your if clause:

yourButton.setEnabled(false); // yourButton is the name of your button!

huangapple
  • 本文由 发表于 2020年8月12日 18:30:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63374664.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定