javiersantos / MaterialStyledDialogs

A library that shows a beautiful and customizable Material-based dialog with header. API 14+ required.
Apache License 2.0
1.17k stars 155 forks source link

Get value from edittext #16

Closed FeedyFeedback closed 8 years ago

FeedyFeedback commented 8 years ago

How can I get the value of an editText when I'm using a custom view?

This is my function code:

function askLogin(View view) { LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View customView = inflater.inflate(R.layout.login, null); EditText school = (EditText) customView.findViewById(R.id.editTextSchool); new MaterialStyledDialog(this) .setDescription("Vul hier uw school en koppelcode in.") .setCustomView(customView) .setIcon(R.drawable.z) .show(); }

This is the function I am calling when I click on the login button:

function logMeIn(View view) { EditText school = (EditText) findViewById(R.id.editTextSchool); System.out.println(schoolf.getText()); }

And this is my login.xml

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

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/editTextSchool"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="School" />

    <EditText
        android:id="@+id/editTextKoppelcode"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Koppelcode"
        android:layout_below="@+id/editTextSchool"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Login"
    android:id="@+id/button20"
    android:layout_gravity="center_horizontal"
    android:onClick="aapje2"
    android:layout_centerHorizontal="true" />

What am I doing wrong?

javiersantos commented 8 years ago

You can do something like this.

public void askLogin(View view) {
       LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       View customView = inflater.inflate(R.layout.login, null);

       final EditText school = (EditText) customView.findViewById(R.id.editTextSchool);
       final Button btn = (Button) customView.findViewById(R.id.button20);

       btn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                     logMeIn(school.getText().toString());
              }
       });

       new MaterialStyledDialog(this)
              .setDescription("Vul hier uw school en koppelcode in.")
              .setCustomView(customView)
              .setIcon(R.drawable.z)
              .show();
}
public void logMeIn(String string) {
       System.out.println(school);
}

Hope it helps you.