/
AlertDialogActivity.java
102 lines (85 loc) · 2.79 KB
/
AlertDialogActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.example.alertdialogtest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
public class AlertDialogActivity extends Activity {
private Button createDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
createDialog = (Button) findViewById(R.id.dialog);
createDialog.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final EditText editText = new EditText(AlertDialogActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
editText.setLayoutParams(lp);
AlertDialog.Builder builder = new AlertDialog.Builder(
AlertDialogActivity.this);
builder.setTitle(R.string.dialogTitle).setView(editText);
builder.setPositiveButton(R.string.ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
Toast.makeText(AlertDialogActivity.this,
editText.getText().toString(),
Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
}
});
final AlertDialog dialog = builder.create();
editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if (editText == null
|| editText.getText().toString().trim()
.equals("")) {
dialog.getButton(AlertDialog.BUTTON_POSITIVE)
.setEnabled(false);
} else {
dialog.getButton(AlertDialog.BUTTON_POSITIVE)
.setEnabled(true);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
dialog.show();
// must use (dialog.getButto() to get the button not reference)
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.alert_dialog, menu);
return true;
}
}