带输入框(EditText)的AlertDialog
一、Main Class:
public void search() {
Builder dialog = new AlertDialog.Builder(this);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout layout = (LinearLayout)inflater.inflate(R.layout.dialogview, null);
dialog.setView(layout);
et_search = (EditText)layout.findViewById(R.id.searchC);
dialog.setPositiveButton("查找", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String searchC = et_search.getText().toString();
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("search", searchC);
intent.putExtras(bundle);
intent.setClass(ViewResultActivity.this, SearchResult.class);
ViewResultActivity.this.startActivity(intent);
}
});
dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.show();
}
二、dialogview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
>
<EditText
android:id = "@+id/searchC"
styles = "@style/edittext_style1"
android:layout_width = "200px"
android:layout_height = "wrap_content"
android:gravity = "left"
/>
</LinearLayout>
public void search() {
Builder dialog = new AlertDialog.Builder(this);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout layout = (LinearLayout)inflater.inflate(R.layout.dialogview, null);
dialog.setView(layout);
et_search = (EditText)layout.findViewById(R.id.searchC);
dialog.setPositiveButton("查找", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String searchC = et_search.getText().toString();
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("search", searchC);
intent.putExtras(bundle);
intent.setClass(ViewResultActivity.this, SearchResult.class);
ViewResultActivity.this.startActivity(intent);
}
});
dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.show();
}
二、dialogview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
>
<EditText
android:id = "@+id/searchC"
styles = "@style/edittext_style1"
android:layout_width = "200px"
android:layout_height = "wrap_content"
android:gravity = "left"
/>
</LinearLayout>
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步