Skip to content

Commit 3b702d6

Browse files
author
silicon ruby
committed
refs #2 AlertDialog Style
1 parent ca4c23b commit 3b702d6

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

AlertDialogActivity.java

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

0 commit comments

Comments
 (0)