让这个类代码创建一个对话框:
public class DConce extends DialogFragment{
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder dshow = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
dshow.setView(inflater.inflate(R.layout.dialogconc, null))
.setPositiveButton("Send", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//TODO
}
})
.setNegativeButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
DConce.this.getDialog().cancel();
}
});
return dshow.create();
}
public static DConce newInstance(){
DConce arg = new DConce();
return arg;
}
我正在尝试使用EditText从用户输入中获取一些数据。该对象是在xml上创建的。我试图用
来调用它EditText dlgText = (EditText) Dconce.findViewById(R.id.txconc);
但它在对话框中找不到findViewByid。这个视图不包含在这里吗? dshow.setView(inflater.inflate(R.layout.dialogconc,null))
请帮忙吗?提前致谢。
PS:对话框创建得很好,我可以看到EditText。我只是无法使用它。
答案 0 :(得分:1)
我在对话框中对我的视图进行了一些修改,并且它有效,如下所示:
LayoutInflater adbInflater = LayoutInflater.from(getActivity());
View editTextLayout = adbInflater.inflate(R.layout.dialogconc, null);
EditText dlgText = (EditText ) editTextLayout .findViewById(R.id.txconc);
适合我:
public class DConce extends DialogFragment {
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder dshow = new AlertDialog.Builder(getActivity());
LayoutInflater adbInflater = LayoutInflater.from(getActivity());
final View editTextLayout = adbInflater.inflate(R.layout.edittext_layout, null);
dshow.setView(editTextLayout)
.setPositiveButton("Send", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
EditText dlgText = (EditText)editTextLayout.findViewById(R.id.editText1);
String string = dlgText.getText().toString();
Log.d("sadsad", string);
}
})
.setNegativeButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
DConce.this.getDialog().cancel();
}
});
return dshow.create();
}
public static DConce newInstance(){
DConce arg = new DConce();
return arg;
}
}
致电活动:
FragmentManager manager = getSupportFragmentManager();
DialogFragment Dialog = DConce.newInstance();
Dialog.show(manager, "tag");
答案 1 :(得分:1)
您应该尝试EditText dlgText = (EditText) dshow.findViewById(R.id.txconc);
答案 2 :(得分:1)
AlertDialog.Builder editalert = new AlertDialog.Builder(ActivityName.this);
editalert.setTitle("My Title");
final EditText input = new EditText(ActivityName.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT);
input.setLayoutParams(lp);
editalert.setView(input);
editalert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String name= input.getText().toString();
}
});
editalert.show();
您还可以使用自定义布局
对话框您的dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText1"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:text="OK" />
<Button
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/ok"
android:layout_alignBottom="@+id/ok"
android:layout_marginLeft="35dp"
android:layout_toRightOf="@+id/ok"
android:text="CANCEL" />
</RelativeLayout>
在您的主要活动中
Button b= (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showpopup();
}
});
}
public void showpopup()
{
final Dialog d = new Dialog(MainActivity.this);
d.setTitle("my title");
d.setContentView(R.layout.dialog);
// Thank you Button Listener. On Click Goes to Home Screen
Button ok = (Button) d.findViewById(R.id.ok);
final EditText ed= (EditText) d.findViewById(R.id.editText1);
ok.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String s= ed.getText().toString();
}
});
Button cancel = (Button) d.findViewById(R.id.cancel);
ok.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
d.cancel();
}
});
d.show();
}