如何在对话框中放置EditText的值

时间:2017-02-04 18:04:59

标签: java android alertdialog

enter image description here

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

当用户输入" abc"时,它只打印EditText中的ascii值..现在我希望使用这些值,用户可以获得这些值的完整详细信息在SAMPLE图像中提到的单独对话框中......

以下是代码:

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

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

    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String a = editText1.getText().toString();
            byte b[]=a.getBytes();
            char ch[]=a.toCharArray();
            for(int i=0;i<ch.length;i++)
            {

                editText2.setText(editText2.getText()+" "+String.valueOf(b[i]));
            }
        }
    });
    btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Ascode.this);


                String a;
                a = editText1.getText().toString();
                String s[]=a.split("\\s");
                byte b[]=a.getBytes();
                    char ch[]=a.toCharArray();

                    for(int i=0;i<a.length();i++)
                    {
                        s[i] = "Value of: \n"+ch[i]+ "is" +b[i]+"\n";
                    }

                    alertDialogBuilder.setTitle("Detailed Output is");
                    alertDialogBuilder.setCancelable(false);
                    alertDialogBuilder.setMessage(String.format(s[i]))
                            .setNeutralButton("Ok", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    dialog.cancel();
                                }
                            });

                    AlertDialog alertDialog = alertDialogBuilder.create();
                    alertDialog.show();
            }
        });

2 个答案:

答案 0 :(得分:1)

  • 可能是你转换你的价值。但在此之前你得到的价值(用户输入),对吧?所以请保持/使用它。
  • 您知道如何显示警报吗?使用自定义警报对话框,在那里使用几个视图来设置您捕获的文本/数据(如果您只想显示它们不需要在那里使用“编辑文本”视图),您就可以开始使用了。

实施例: dialog_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TextView
        android:hint="textViewOne"
        android:id="@+id/textViewOne"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:hint="textViewTwo"
        android:id="@+id/textViewTwo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:hint="textViewThree"
        android:id="@+id/textViewThree"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

警报

    LayoutInflater inflater = getLayoutInflater();
    View dialogLayout = inflater.inflate(R.layout.dialog_layout, null);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Details Out Put");
    TextView textViewOne = (TextView) dialogLayout.findViewById(R.id.textViewOne);
    textViewOne.setText("One"); <--- here set the text you got! Insted of One like editText1.getText().toString()
    // add other two text views as the same way
    builder.setView(dialogLayout);
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        }
    });
    builder.show()

注意:我已经使用了TextView,因为我没有看到编辑您只想显示的文字的点!如果您想用EditText次观看

替换它们

我不太确定你的算法是什么。但是如果你想将String转换为ascii字节数组,这就是你需要转换的整个代码,并设置一个输入的值。

String stringOfEditTextOne = editTextOne.getText().toString();
byte[] biteArrayOne = stringOfEditTextOne.getBytes(StandardCharsets.US_ASCII);
System.out.println("BiteArrayOut >" + biteArrayOne.toString());

// now you only need to set this value
// textViewOne.setText(biteArray.toString());

编辑:以下是您需要做的所有事情

ArrayList<String> userInputOneAsciiArrayListString = new ArrayList<String>();
//...
String userInputOne = "Charu"; // use edit text and get the user input
for (int i = 0; i< userInputOne.length() ; i++){
     char character =  userInputOne.charAt(i);  // get each character
     int ascii = (int) character;
     userInputOneAsciiArrayListString.add(ascii+"");
}
System.out.println("ascii Char Value >"+userInputOneAsciiArrayListString);
String formatStringForFirsInput = userInputOneAsciiArrayListString.toString()
        .replace(",", "")  //remove the commas
        .replace("[", "")  //remove the right bracket
        .replace("]", "")  //remove the left bracket
        .trim();           //remove trailing spaces from partially initialized arrays

// now directly set this formatStringForFirsInput value to textView

答案 1 :(得分:1)

Hy @Rohan,对不起,但是我没有了。所以,这是我的解决方案,我希望我能理解你的问题。这是btn2:

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


            String a;
            a = editText1.getText().toString();

            a = a.replace(" ","");
            byte b[] = a.getBytes();
            String[] s = a.split("(?!^)");
            String message = "";

            for(int i=0;i<s.length();i++)
            {
                message += "Value of: \n"+s[i]+ " is " +b[i]+"\n";
            }

            alertDialogBuilder.setTitle("Detailed Output is");
            alertDialogBuilder.setCancelable(false);
            alertDialogBuilder.setMessage(message)
                    .setNeutralButton("Ok", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });

            AlertDialog alertDialog = alertDialogBuilder.create();
            alertDialog.show();
        }
});

希望这有帮助