如何在对话框中输入编辑文本的值 [英] how to put values of edit texts in the dialogue box

查看:97
本文介绍了如何在对话框中输入编辑文本的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过使用在菜单中输入的值,提供由用户在EditText中以单独方式在详细对话框中输入的输出.编辑文本.任何帮助将不胜感激.

I want to give the output, which was entered by the user in EditText, in a detailed manner in a separate dialog box, by using the values of those entered values in the edittext. Any help will be appreciated.

这是代码:

EditText editText1,editText2,editText3;
Button btn,btn2;

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

    btn = (Button) findViewById(R.id.btn);
    btn2 = (Button) findViewById(R.id.btn2);

    editText1 = (EditText) findViewById(R.id.editText2);
    editText2 = (EditText) findViewById(R.id.editText3);
    editText3 = (EditText) findViewById(R.id.editText4);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            calculate();
        }
    });

    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);

            String s = " (Result) ";
            alertDialogBuilder.setTitle("Detailed Output is");
            alertDialogBuilder.setCancelable(false);
            alertDialogBuilder.setMessage("(The code should be like as follows)\n\n"+"sum of entered number is:"+s+"\n"+"multiply of entered number is:"+s+"\n"+"subtraction of entered number is:"+s)

                    .setNeutralButton("Ok", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
            AlertDialog alertDialog = alertDialogBuilder.create();
            alertDialog.show();

        }
    });
}
    public void calculate() {

        String a = editText1.getText().toString();
        String b = editText2.getText().toString();
        editText3.setText(String.valueOf(Integer.parseInt(a)+ Integer.parseInt(b))+"\n"+String.valueOf(Integer.parseInt(a)* Integer.parseInt(b))+"\n"+String.valueOf(Integer.parseInt(a)-Integer.parseInt(b)));
    }
}

推荐答案

好,尝试这种方式:

首先,在MainActivity的onCreate()方法中声明两个EditText(要对它们进行求和,减去和乘以等)作为最终操作:

First, declare your two EditText (those you want to sum, subtract and multiply the values, etc.) as final inside the onCreate() method of your MainActivity:

final EditText editText1 = (EditText) findViewById(R.id.editText2);
final EditText editText2 = (EditText) findViewById(R.id.editText3);

第二,为您的btn2.setOnClickListener尝试以下代码:

Second, try this code for your btn2.setOnClickListener:

btn2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);

        int myNum_one, myNum_two, myNum_three;

        try {

            myNum_one = Integer.parseInt(editText1.getText().toString());
            myNum_two = Integer.parseInt(editText2.getText().toString());

            String s = "Sum of entered number is: %d \n multiply of entered number is: %d \n subtraction of entered number is: %d";
            int sum = myNum_one + myNum_two;
            int multiply = myNum_one * myNum_two;
            int subtraction = myNum_one - myNum_two;
            alertDialogBuilder.setTitle("Detailed Output is");
            alertDialogBuilder.setCancelable(false);
            alertDialogBuilder.setMessage(String.format(s, sum, multiply, subtraction))
                  .setNeutralButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });
            AlertDialog alertDialog = alertDialogBuilder.create();
            alertDialog.show();

        } catch(NumberFormatException nfe) {
            Log.d("TAG","Could not parse " + nfe);
        } 

    }
});

这篇关于如何在对话框中输入编辑文本的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆