2017-02-03 14 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. 예 ..u 정확히 무슨 뜻인지 이해했습니다. – Rohan

답변

0

좋아,이 방법을 시도해보십시오

먼저, 두 글고 치기를 선언) 당신의 MainActivity의 방법을합니다 (에서 onCreate 내에서 마지막으로 (당신이 합계 빼고 값 등을 곱 할 것) :

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를 최종으로 만들 수 없으므로 오류가 발생합니다. 두 번째로 최종 작성하지 않고 사용하면 두 번째로 .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

은 먼저 코드에 msg를 쓰지 않습니다. 응용 프로그램을 다른 언어로 번역하려는 경우 옵션이 없기 때문에 더 나은 방법이 사용됩니다. 문자열 .xml 파일 저장 문자열에 문자열

좋은 방법은 msg를 문자열에 추가하는 것입니다. .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);