简体   繁体   English

如何设置警报框

[英]how to set an alert box

in my app i have placed three edit boxes with a ok button. 在我的应用程序中,我已经放置了三个带有确定按钮的编辑框。 Before pressing the ok button the user must enter all the edit text fields, if any of the edit text is been left empty i want to give an alert box. 在按下确定按钮之前,用户必须输入所有编辑文本字段,如果任何编辑文本保留为空,我想提供一个警告框。

in the edit text box the field name such as "Name", "Age" should be in diminished and when it is clicked it must get disappeared. 在编辑文本框中,字段名称,如“名称”,“年龄”应该减少,当它被点击它必须消失。

How to do this , pls anyone help me 怎么做,请任何人帮助我

Check length: 检查长度:

if (edit1.getText().length() > 0 && edit2.getText.length() > 0 && edit3.getText.length() > 0) {
    // Do your normal code here 
} else {
    // Call your alert dialog creation
}

Diminished? 而减少? You mean a hint (which is shown when there's no text in the field)? 你的意思是一个提示(当字段中没有文字时显示)? This is done like this... 这是这样做的......

XML inside the EditText field: EditText字段中的XML:

android:hint="Clear by clicking"

Source code: 源代码:

nameEditText.setHint("Clear by clicking");

Remove text on click (if you have already created an EditText field called nameEditText): 单击时删除文本(如果已创建名为nameEditText的EditText字段):

    // Clear text when clicked
    nameEditText.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            nameEditText.setText("");
        }
    });

And then do what Vladimir said 然后做弗拉基米尔说的话

Just use AlertDialog . 只需使用AlertDialog Check all the conditions and if there is an error build a dialog and show it. 检查所有条件,如果有错误,请构建一个对话框并显示它。

you can design this ui in your activity, and this activity should have the theme android:theme="@android:style/Theme.Dialog" . 你可以在你的活动中设计这个ui,这个活动的主题应该是android:theme="@android:style/Theme.Dialog" And when you want your activity to disappear. 当你希望你的活动消失时。 simpally call finish() simpally call finish()

its simple.. for name checking : 它的简单..用于名称检查:

if(editname.getText().tostring().length==0)

show alert... 显示提醒...

AlertDialog.Builder builder=new AlertDialog.Builder(context);
builder.setTitle("something,");
builder.setMessage("something..");
builder.show():

you can also add button...by 你也可以添加按钮...

builder.setNeutralButton("name",new DialogInterface.onclick
............}

just try this 试试吧

   AlertDialog.Builder alert = new AlertDialog.Builder(this);
                alert.setMessage("Error Msg).setPositiveButton("OK", alertClickListener).show();

        DialogInterface.OnClickListener alertClickListener = new DialogInterface.OnClickListener()
    {
        @Override
        public void onClick(DialogInterface dialog, int which) 
        {

        }
    };

In your Ok Button OnClick do the following 在您的Ok按钮OnClick执行以下操作

    if (et1.getText().toString().length() != 0) {
     emailid = String.valueOf(et1.getText());
    }
    if((emailid==null|| emailid=="")){
        tvError.setVisibility(View.VISIBLE);
        tvError.setText("All fields are Mandatory");
        Toast.makeText(Signin.this,"All fields are Mandatory", Toast.LENGTH_SHORT).show();
    }else{
              // Your operation
         }

where et1 is Edit box 1,emailid is String .. 其中et1是编辑框1,emailid是String ..

In your XML file create a textview with option android:visibility="GONE".. 在您的XML文件中创建一个带有选项android:visibility =“GONE”的textview ..

Now in if part Make that textview visible if error occurs or do your process in Else.. 现在,如果部分使得文本视图在发生错误时可见或在Else中执行您的过程。

Also you can keep toast mesaage... 你也可以保持烤面包...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM