显示消息而不关闭Android中的实际对话框

时间:2013-10-14 09:28:48

标签: java android

我要对Android应用程序进行一对修复虽然我并不真正了解Android,但我遇到了一些我认为应该不那么困难的问题,我只想要一个OK按钮被按下并且某些条件没有被满足它显示一条消息并保持在同一个屏幕上直到数据正确或用户取消,但我已经尝试了一段时间,无论我尝试它总是显示消息然后出现白屏,甚至试图在互联网上搜索示例。

这是我的代码:

final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

        LinearLayout layout = new LinearLayout(this);
        LinearLayout.LayoutParams parms = new   LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setLayoutParams(parms);

        layout.setGravity(Gravity.CLIP_VERTICAL);
        layout.setPadding(2, 2, 2, 2);

        TextView tv = new TextView(this);
        tv.setText("Es necesario rellenar los datos solicitados a continuación para poder realizar su primer canje");
        tv.setPadding(40, 40, 40, 40);
        tv.setGravity(Gravity.CENTER);
        tv.setTextSize(20);

        EditText et = new EditText(this);
        String etStr = et.getText().toString();
        TextView tv1 = new TextView(this);
        tv1.setText("Nombre completo");

        EditText et2 = new EditText(this);
        String etStr2 = et2.getText().toString();
        TextView tv2 = new TextView(this);
        tv2.setText("Teléfono");

        final EditText et3 = new EditText(this);
        String etStr3 = et3.getText().toString();
        TextView tv3 = new TextView(this);
        tv3.setText("Correo electrónico");

  LinearLayout.LayoutParams tv1Params = new    LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,    LinearLayout.LayoutParams.WRAP_CONTENT);
        tv1Params.bottomMargin = 5;
        layout.addView(tv1,tv1Params);
        layout.addView(et, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,  LinearLayout.LayoutParams.WRAP_CONTENT));
        layout.addView(tv2,tv1Params);
        layout.addView(et2, new   LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        layout.addView(tv3,tv1Params);
        layout.addView(et3, new  LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));


        alertDialogBuilder.setView(layout);
        alertDialogBuilder.setTitle("hola");
        // alertDialogBuilder.setMessage("Input Student ID");
        alertDialogBuilder.setCustomTitle(tv);


        // alertDialogBuilder.setMessage(message);
        alertDialogBuilder.setCancelable(true);

        // Setting Negative "Cancel" Button
        alertDialogBuilder.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {

 alertDialogBuilder.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {

[more code here]

 alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {



                matcher = pattern.matcher(et3.getText().toString());

                if (matcher.matches())
                {
                    [more code here]
                    }
                    else
                     {
   Toast.makeText( contexto, "Por favor, introduzca un e-mail válido", Toast.LENGTH_LONG).show();
                    }

希望你能帮助我解决这个问题,因为我觉得从一开始就学习android以便在5分钟或更短时间内用其他编程语言做一些我不太了解所有

3 个答案:

答案 0 :(得分:0)

首先使用xml创建布局并像这样膨胀视图:

LayoutInflater li = (LayoutInflater) <ACTIVITY_NAME>.this
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = li.inflate(R.layout.<LAYOUT_NAME>, parent, false);

View view = li.inflate(R.layout.<LAYOUT_NAME>, null);

如果没有父视图将膨胀的视图附加到。然后,您可以通过执行以下操作来编辑视图中的对象:

EditText edit = (EditText) findViewById(R.id.edit1);
edit.setText("example");

这样做只会让您的代码更清晰。

方法:setPositiveButtonsetNeutralButtonsetNegativeButton被编码,以便在按下它们时,对话框将在监听器中执行完代码后关闭。

如果你的Android应用程序在主线程上运行超过5秒,那么该应用程序将抛出错误消息,表明应用程序不再响应。如果您希望执行长时间的操作,则应使用AsyncTaskService

我相信你想要有某种进步条。我会把你链接到一个 教程将向您展示如何实现这一方面。 Check here

希望这能指出你正确的方向。

答案 1 :(得分:0)

创建两个实例变量或类变量,如下所示

private Toast toast;
private boolean stop = false;

编写一个名为this

的方法
private void showInfiniteToast() {
    stop = false;
    Thread t = new Thread() {
        public void run() {
            try {
                while (true) {
                    if (!stop) {
                        toast.show();
                    } else {
                        toast.cancel();
                        return;
                    }
                    sleep(1850);
                }
            } catch (Exception e) {
                Log.e("Infinite Toast", "Error "+ e.getLocalizedMessage());
            }
        }
    };
    t.start();
}

现在在oncreate中创建toast并调用此方法

toast = Toast.makeText(getApplicationContext(), "Test", Toast.LENGTH_LONG);
showInfiniteToast();

现在,如果您想更改Toast消息,请使用此

toast.setText("message");

要停止致电任何这些

//Call anyone of them
stop = true;
toast.cancel();

要实现您自己的自定义视图,请使用此

View mView; 
LayoutInflater inflater = (LayoutInflater)   getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
mView = inflater.inflate(R.layout.mylayout, null);
toast.setView(mView);

这是完整的文件

public class MainActivity extends Activity implements OnClickListener {

Button btnChange, btnStop, btnShow;
private Toast toast;
private boolean stop = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btnShow = (Button) findViewById(R.id.btnShow);
    btnShow.setOnClickListener(this);
    btnChange = (Button) findViewById(R.id.btnChange);
    btnChange.setOnClickListener(this);
    btnStop = (Button) findViewById(R.id.btnStop);
    btnStop.setOnClickListener(this);

    toast = Toast.makeText(getApplicationContext(), "Test",
            Toast.LENGTH_LONG);
    showInfiniteToast();

}


private void showInfiniteToast() {
    stop = false;
    Thread t = new Thread() {
        public void run() {
            try {
                while (true) {
                    if (!stop) {
                        toast.show();
                    } else {
                        toast.cancel();
                        return;
                    }
                    sleep(1850);
                }
            } catch (Exception e) {
                Log.e("Infinite Toast", "Error "+ e.getLocalizedMessage());
            }
        }
    };
    t.start();
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btnShow:
        showInfiniteToast();
        break;
    case R.id.btnChange:
        toast.setText("Added");

        break;
    case R.id.btnStop:
        stop = true;
        toast.cancel();
        break;

    default:
        break;
    }
}

}

答案 2 :(得分:0)

创建一个dialogBu​​ilder,并覆盖负面和正面按钮,并在点击监听器上做任何你想做的事情。这将阻止对话框关闭。

像这样: 创建构建器,初始化它,将其设置为例如:

builder.setView(view);
builder.setCancelable(false);

覆盖ondismiss侦听器和onshow侦听器,如:

builder.setOnDismissListener(new OnDismissListener() {
    @Override
    public void onDismiss(DialogInterface dialog) {
        if(!errorFlag) {
            dialog.dismiss();
        }
    }   
});



builder.setOnShowListener(new OnShowListener() {
    @Override
    public void onShow(DialogInterface dialog) {
        Button b = builder.getButton(AlertDialog.BUTTON_POSITIVE);
        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                // write the logic here, and maintain a flag.
                // if the flag is true then only dismiss the dialog else show another one
            }

也覆盖否定按钮。

使用builder.show()

显示构建器