我想设计一个自定义对话框,其中包含一个带有用户输入的编辑文本,我也希望设计自定义布局以供对话使用,任何人都可以帮助
答案 0 :(得分:0)
// Try this way,hope this will help you to solve your problem...
**activity_main.xml**
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/txtDialogValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnOpenDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Open Dialog"/>
</LinearLayout>
**custom_dialog.xml**
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/txtHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:padding="5dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:orientation="vertical"
android:gravity="center">
<EditText
android:id="@+id/edtInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Input"/>
<Button
android:id="@+id/btnOk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Ok"/>
</LinearLayout>
</LinearLayout>
**MainActivity.java**
public class MainActivity extends FragmentActivity {
private TextView txtDialogValue;
private Button btnOpenDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtDialogValue = (TextView) findViewById(R.id.txtDialogValue);
btnOpenDialog = (Button) findViewById(R.id.btnOpenDialog);
btnOpenDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customDialog(MainActivity.this,"Enter value","Ok",R.layout.custom_dialog,new CustomDialogClickListener() {
@Override
public void onClick(String input) {
txtDialogValue.setText(input);
}
});
}
});
}
public void customDialog(final Context context,final String title,final String buttonCaption, final int layoutID,final CustomDialogClickListener target) {
((Activity)context).runOnUiThread(new Runnable() {
@Override
public void run() {
final Dialog dialog = new Dialog(context,android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(layoutID);
final TextView txtHeader = (TextView) dialog
.findViewById(R.id.txtHeader);
final EditText edtInput = (EditText) dialog
.findViewById(R.id.edtInput);
final Button btnOk = (Button) dialog
.findViewById(R.id.btnOk);
txtHeader.setText(title);
btnOk.setText(buttonCaption);
btnOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
target.onClick(edtInput.getText().toString());
dialog.dismiss();
}
});
dialog.show();
}
});
}
interface CustomDialogClickListener{
public void onClick(String input);
}
}
答案 1 :(得分:0)
Try this one:
public void dialogBox(){
Dialog mDialog = new Dialog(this.getActivity());
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.setContentView(R.layout.fragment_layout);
mDialog.show();
EditText mEditText = (EditText) findViewById(R.id.id_number_custom);
}
创建一个您需要展示的布局。