AlertDialog with EditText


import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private Button myButton;
private String myText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

myButton=findViewById(R.id.button);

myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder mydialog = new AlertDialog.Builder(MainActivity.this);
mydialog.setTitle("Weight in Kilograms? Example 21.6");

final EditText weightInput = new EditText(MainActivity.this);
weightInput.setInputType(InputType.TYPE_CLASS_PHONE);
mydialog.setView(weightInput);

mydialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
myText=weightInput.getText().toString();
Toast.makeText(MainActivity.this, "Weight is "
+myText, Toast.LENGTH_LONG).show();
}
});

mydialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
mydialog.show();
}
});
}
}

Deixe uma resposta

O seu endereço de email não será publicado.