有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何将EditText的值放入对话框

enter image description here

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

例如,当用户输入“abc”时,它只在EditText中打印他们的ascii值。。现在我希望使用这些值,用户可以在一个单独的对话框中获得这些值的全部细节,如示例图像中所述

代码如下:

    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) 个答案

  1. # 1 楼答案

    • 也许你能转换你的价值。但在此之前,你会得到这个值(用户输入),对吗?所以保留/使用它
    • 你知道怎么显示警报吗?使用自定义警报对话框使用两个视图设置从用户输入中捕获的文本/数据(如果您只想显示它们,不需要使用编辑文本视图),这样就可以了

    例如: 对话框的布局。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视图替换它们


    我不太确定你的算法是什么。但是,如果你想将字符串转换成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
    
  2. # 2 楼答案

    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();
            }
    });
    

    希望这有帮助