如何制作一个只包含一个 EditText 的窗口对话框,并且弹出一个像警报一样的警报,以从 Android 下的用户获取文本。?
我已经检查了这个Get text from an EditText
如何制作一个只包含一个 EditText 的窗口对话框,并且弹出一个像警报一样的警报,以从 Android 下的用户获取文本。?
我已经检查了这个Get text from an EditText
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Title");
alert.setMessage("Message");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText();
// Do something with value!
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
尝试添加
android:theme="@android:style/Theme.Dialog"
EditText
在包含and 调用requestWindowFeature(Window.FEATURE_NO_TITLE)
before的活动清单中setContentView
。
这应该在警报对话框中显示活动。
希望这可以帮助。
activity_login.xml // place inside layout folder
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/btnDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="clickme"
android:textColor="#fff"
android:textStyle="bold" >
</Button>
</RelativeLayout>
dialog_box.xml /// layout for dialog,Place it inside drawable folder
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fff" >
<LinearLayout
android:id="@+id/linearlayout"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:gravity="center" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DialogBox"
android:textColor="#000"
android:textSize="14sp"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="@+id/txtview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/linearlayout"
android:layout_marginBottom="3dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="3dp"
android:text="Enter E-mail Address"
android:textColor="#000000"
android:textSize="14sp"
android:textStyle="bold" />
<EditText
android:id="@+id/edtFEmail"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:layout_alignParentLeft="true"
android:layout_below="@id/txtview"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="3dp"
android:gravity="center"
android:hint="E-mail"
android:inputType="textEmailAddress" >
</EditText>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/edtFEmail"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:weightSum="4" >
<Button
android:id="@+id/btnSaveDialog"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="3dp"
android:layout_weight="2"
android:text="Submit"
android:textColor="#000"
android:textSize="16dip" />
<Button
android:id="@+id/btnCancelDialog"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_marginBottom="2dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="2dp"
android:layout_weight="2"
android:text="Cancel"
android:textColor="#000"
android:textSize="16dip" />
</LinearLayout>
</RelativeLayout>
LoginActivity .java
public class LoginActivity extends Activity {
Context ctx;
EditText edtFEmailDialog;
Button btnShowDialog, btnCancelDialog, btnSaveDialog;
Dialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ctx = this;
init();
btnSaveDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String sfemail = edtFEmailDialog.getText().toString();
if (edtFEmailDialog.getText().toString().equals("")
|| edtFEmailDialog.getText().toString().equals("null")) {
edtFEmailDialog.requestFocus();
edtFEmailDialog.setError("Please Enter E-mail Address");
return;
}
Toast.makeText(ctx, edtFEmailDialog.getText().toString(), 1000)
.show();
}
});
btnCancelDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
edtFEmailDialog.setText("");
dialog.dismiss();
}
});
btnShowDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.show();
}
});
}
public void init() {
dialog = new Dialog(LoginActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_box);
dialog.setCancelable(true);
btnShowDialog = (Button) dialog.findViewById(R.id.btnDialog);
btnCancelDialog = (Button) dialog.findViewById(R.id.btnCancelDialog);
btnSaveDialog = (Button) dialog.findViewById(R.id.btnSaveDialog);
edtFEmailDialog = (EditText) dialog.findViewById(R.id.edtFEmail);
}
}
cheers guys