如何在MainActivity上显示使用EditText在自定义alertDialogFragment上插入的数字?

时间:2016-05-17 04:10:30

标签: android android-edittext android-alertdialog android-dialogfragment

我正在使用带有alertdialog的自定义editText来插入数字,alertdialog正在运行,但是当我点击“正”按钮时没有任何反应,它应该显示插入的值,在main_activity.xml

似乎我的界面无法正常工作。我不知道我做错了什么,这是我的代码:

TestAlertDialogFragment.java

public class TestAlertDialogFragment extends DialogFragment {

// Use this instance of the interface to deliver action events

TestAlertDialogListener mListener;

public interface TestAlertDialogListener {
    public void onDialogPositiveClick(DialogFragment dialog, int i, String string);

    public void onDialogNegativeClick(DialogFragment dialog);
}


@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction


    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();


    builder.setView(inflater.inflate(R.layout.fragment_test_dialog, null))

            .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int id) {

                    final EditText editText = (EditText) getDialog().findViewById(R.id.number);
                    int i = 0;
                    String string;
                    if (editText != null) {
                        string = editText.getText().toString();
                        i = Integer.parseInt(string);

                    }
                    if (mListener != null) {
                        mListener.onDialogPositiveClick(TestAlertDialogFragment.this, i, string);
                    }
                }
            })

            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int id) {
                    // User cancelled the dialog
                    if (mListener != null) {
                        mListener.onDialogNegativeClick(TestAlertDialogFragment.this);
                    }
                }
            });
    // Create the AlertDialog object and return it
    AlertDialog dialog = builder.create();

    // makes the soft-keyboard shows when edittext is focused
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

    return dialog;
}
}

MainActivity.java

public class MainActivity extends FragmentActivity
        implements TestAlertDialogFragment.TestAlertDialogListener {
int i=0;

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);


public void showTestAlertDialog(View v) {
    // Create an instance of the dialog fragment and show it

    DialogFragment d = new TestAlertDialogFragment();
    d.show(getSupportFragmentManager(), "testAlertDialog");
    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

}

public void setSelectedNumber(String string, int i) {
    TextView textView = (TextView) findViewById(R.id.test_number);
    textView.setText(string);
    TextView textView1 = (TextView) findViewById(R.id.test_string);
    String string2 = i;
    textView1.setText(string2);
}


@Override
    public void onDialogPositiveClick(DialogFragment dialog, int i, String string) {
        // User touched the dialog's positive button
        setSelectedNumber(string, i);
    }

    @Override
    public void onDialogNegativeClick(DialogFragment dialog) {
        // User touched the dialog's negative button

    }
}

activity_main.xml中

<LinearLayout
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:orientation="vertical"
tools:context=".MainActivity">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Input Number"
    android:layout_marginTop="20dp"
    android:id="@+id/button"
    android:onClick="showTestAlertDialog"
    android:layout_gravity="center_horizontal" />

<TextView
    android:id="@+id/test_number"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="20dp"
    android:textAppearance="?android:attr/textAppearanceMedium"/>

<TextView
    android:id="@+id/test_string"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="20dp"
    android:textAppearance="?android:attr/textAppearanceMedium"/>

</LinearLayout>

fragment_alert_dialog.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="com.example.android.datepickertest.InserirPrazoDialogFragment">

<!-- TODO: Update blank fragment layout -->

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="INSERT NUMBER:"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:paddingTop="5dp"
    android:textColor="#40C4FF"
    android:textStyle="bold" />

<View
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:layout_gravity="center_horizontal"
    android:layout_marginLeft="3dp"
    android:layout_marginRight="3dp"
    android:background="#40C4FF"/>


<EditText
    android:id="@+id/number"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:ellipsize="middle"
    android:focusable="true"
    android:padding="10dp"
    android:layout_marginBottom="5dp"
    android:textSize="25dp"
    android:ems="2"
    android:inputType="number" />

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

您未在“活动”中设置监听器TestAlertDialogListener 。只需进行以下更改即可实现。

TestAlertDialogFragment中创建一个构造函数,它将为我们设置监听器。我没有粘贴对话框的整个代码,只是必不可少的片段。 onCreateDialog方法将保持原样。

public class TestAlertDialogFragment extends DialogFragment {

// Use this instance of the interface to deliver action events

TestAlertDialogListener mListener;

public TestAlertDialogFragment(TestAlertDialogListener mListener)
{

    this.mListener=mListener;

}

public interface TestAlertDialogListener {
    public void onDialogPositiveClick(DialogFragment dialog, int i, String string);

    public void onDialogNegativeClick(DialogFragment dialog);
}

更改您的活动代码:

public void showTestAlertDialog(View v) {
// Create an instance of the dialog fragment and show it

DialogFragment d = new TestAlertDialogFragment(MainActivity.this);
d.show(getSupportFragmentManager(), "testAlertDialog");
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

}