2017-02-03 89 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); 
相關問題