关于自定义对话框错误

时间:2015-10-26 05:42:07

标签: android dialog

我使用Fragmant和三个标签。 我想在第一个标签中输入。 所以我做了源。 但是,如果我点击两次按钮,' alert.show();'使错误。 我发现很多方法,但我无法解决。

Tab1.java

package sm.fo3simultor2;
import...

@SuppressLint("ValidFragment")
public class Tab1 extends Fragment {
Context mContext;

public Tab1(Context context) {
    mContext = context;
}

@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.activity_tab1, null);
final View view2 = inflater.inflate(R.layout.activity_alert1, null);
final Button button1;
final EditText edittext;
final TextView textview;
button1 = (Button) view.findViewById(R.id.button);
edittext = (EditText) view2.findViewById(R.id.editText);
textview = (TextView) view.findViewById(R.id.FinalEP);

final AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
        if (edittext.getText().toString().equals("")) {
        Toast.makeText(getActivity(), "Error..", Toast.LENGTH_SHORT).show();
        dialog.dismiss();
        } else {
        textview.setText(edittext.getText().toString());
        Toast.makeText(getActivity(), "OK.", Toast.LENGTH_SHORT).show();
        dialog.dismiss();}}
        }
    });
alert.setView(view2);
button1.setOnClickListener(new OnClickListener() {
                                       @Override
                                       public void onClick(View v) {
                                       alert.show();}
);
return view;
}
}

activity_alert1

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Input"
    android:id="@+id/textView5"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:id="@+id/editText"
    android:layout_below="@+id/textView5"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

我想制作输入框。 请帮我。 请找到我的错误。

3 个答案:

答案 0 :(得分:1)

使用AlertDialog而不是builder

AlertDialog alert = builder.create (context);

如果是,则检查警报是否已经可以取消警报或显示警报。

if(alert.isShowing()){
    alert.dismiss();
}else{
  alert.show();
 }

答案 1 :(得分:0)

如果想使用对话框获取输入,请使用此...

AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
                alertDialog.setMessage("Your Message");

                final EditText input = new EditText(this);
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
                input.setLayoutParams(lp);
                input.setInputType(InputType.TYPE_CLASS_NUMBER);

                InputFilter[] filter = new InputFilter[1];
                filter[0] = new InputFilter.LengthFilter(3);

                input.setFilters(filter);
                alertDialog.setView(input);

                alertDialog.setPositiveButton("Goto",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which)
                            {
                                    Toast.makeText(getApplicationContext(), input.getText().toString(), Toast.LENGTH_LONG).show();
                            }
                        });

                alertDialog.setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        });

                alertDialog.show();

答案 2 :(得分:0)

您已经构建了alertdialog,但是您还需要创建AlertDialog,所以只需添加它,

AlertDialog alertdialog = alert.create();
alertdialog.show();