简体   繁体   中英

Dialog with multiple textview

i need to add 5 text view in my code. For now there is only one and it is used to put a marker in google maps. I would like that in addition to this you could put another 4 data insertions like (description, age, work, hobbys).Below I also put the custom dialog I made can you check if it's okay? Feel free to ask for anything. How can I do?

                add_address.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            View viewcustom = getLayoutInflater().inflate(R.layout.custom_dialog, null);
            EditText edt1 = view.findViewById(R.id.Description);
            EditText edt2 = view.findViewById(R.id.Age);
            EditText edt3 = view.findViewById(R.id.Hobby);
            EditText edt4 = view.findViewById(R.id.City);
            EditText edt5 = view.findViewById(R.id.address);

            final AlertDialog.Builder alertDialog = new AlertDialog.Builder(requireContext())
                    .setView(viewcustom)
                    .setPositiveButton("Ok", (dialogInterface, i) -> {
                        String description = edt1.getText().toString();
                        String age = edt2.getText().toString();
                        String Hobby = edt3.getText().toString();
                        String City = edt4.getText().toString();
                        String address = edt5.getText().toString();

                        Toast.makeText(requireContext(), description, Toast.LENGTH_SHORT).show();
                firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
                reference = FirebaseDatabase.getInstance().getReference("Users").child(firebaseUser.getUid()).child("address");
                reference.setValue(address);
            });
            alertDialog.setNegativeButton("Cancel", null);
            alertDialog.create().show();
            //startAutoCompleteIntent();
        }
    });

Customdialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Text 1"
        android:id="@+id/Description" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Text 2"
        android:id="@+id/Age" />

    <EditText
        android:id="@+id/Hobby"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:hint="Text 3" />

    <EditText
        android:id="@+id/City"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Text 4" />
    <EditText
        android:id="@+id/Marker"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Text 5" />
</LinearLayout>

You have to provide your own custom view and then you have to retrieve your EditTexts with findViewByID()

View view = getLayoutInflater().inflate(R.layout.custom_dialog, null);
EditText edt1 = view.findViewById(R.id.Description);
// get other EditTexts here
final Dialog alertDialog = new Dialog(activity);
alertDialog.setView(view);
alertDialog.setPositiveButton("Ok", (dialogInterface, i) -> {
    String description = edt1.getText().toString();
    //other stuff
});

Entire sample inspired from your code

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(LayoutInflater.from(this));

        setContentView(binding.getRoot());

        binding.fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                View viewcustom = getLayoutInflater().inflate(R.layout.custom_dialog, null);
                EditText edt1 = viewcustom.findViewById(R.id.Description);
                EditText edt2 = viewcustom.findViewById(R.id.Age);
                EditText edt3 = viewcustom.findViewById(R.id.Hobby);
                EditText edt4 = viewcustom.findViewById(R.id.City);

                final AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this)
                        .setView(viewcustom)
                        .setPositiveButton("Ok", (dialogInterface, i) -> {
                            String description = edt1.getText().toString();
                            String age = edt2.getText().toString();
                            String Hobby = edt3.getText().toString();
                            String City = edt4.getText().toString();

                            Toast.makeText(MainActivity.this, description, Toast.LENGTH_SHORT).show();
                        });
                alertDialog.setNegativeButton("Cancel", null);
                alertDialog.create().show();
            }
        });
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM