2017-02-03 111 views
1

enter image description here我想给一个单独的对话框输出,这是由在EditText上的用户输入,以详细的方式,通过使用这些输入的数值的数值edittext。任何帮助将不胜感激。如何把编辑文本的值在对话框中

下面是代码:

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))); 
    } 
} 
+0

你能解释一下你的问题吗?您是否想要在AlertDialog消息中显示在editText1和editText2中输入的两个数字的总和,乘法和减法? –

+0

@ValentinoS。 yes..u明白正是我的意思 – Rohan

回答

0

好吧,试试这个方法:

首先,声明你的两个EditText上(您想总结,减法和乘法值等)的onCreate内为最终()您的MainActivity的方法:

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

其次,试试这个代码为您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); 
     } 

    } 
}); 
+0

我不能够做的EditText为final,因为它是给errors..and第二件事,如果我不使用在.setNuetralButton做最后的话也正在提供错误.. ..请再次通过我的代码,如果可能的话,因为你几乎在那里.. – Rohan

+0

好吧,我做错了:)我刚刚编辑了答案 –

+0

我也试过了...但对话框不开放,它如果我点击对话按钮 – Rohan

1

你可以通过用户的String.Format你的字符串输入参数进行格式化。

alertDialogBuilder.setMessage(String.format("(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"),editText1.getText().toString(), editText2.getText().toString(), editText3.getText().toString()) 
0

首先不要在代码中编写味精,因为如果你想将你的应用翻译成不同的语言,你没有选择,更好的做法是使用strings.xml文件来存储你的字符串

好方法是将你的味精添加到字符串.xlm工作文件

<string name="myMsg">The code should be like as follows)\n\n sum of entered number is:%1$s \n multiply of entered number is:%2$s\n subtraction of entered number is:%3$s</string> 

,如果你想在代码中使用此:

String msg = String.format(getResources().getString(R.string.myMsg), editText1,editText2,editText3)); 

,并加入到对话:

alertDialogBuilder.setMessage(msg); 
相关问题