如何在MainActivity上显示带有EditText的自定义alertDialogFragment上插入的数字? [英] How to display, on MainActivity, the number inserted on a custom alertDialogFragment with EditText?

查看:66
本文介绍了如何在MainActivity上显示带有EditText的自定义alertDialogFragment上插入的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有editText的自定义alertdialog来插入数字,alertdialog正在工作,但是当我单击肯定按钮时没有任何反应,它应该在main_activity.xml中显示插入的值.

I'm using a custom alertdialog with an editText to insert a number, the alertdialog is working, but nothing happens when I click positive button, it should display the value inserted, in main_activity.xml.

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

It seems that my interface is not working. I don't know what I'm doing wrong, here is my code:

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>

推荐答案

您没有在活动"中设置监听器TestAlertDialogListener.只需进行以下更改,您就可以正常工作.

You are not setting listener TestAlertDialogListener in your Activity. Just make below changes and you will get it working.

在您的TestAlertDialogFragment中创建一个构造函数,该构造函数将为我们设置侦听器.我没有粘贴对话框的整个代码,只是粘贴了一些重要的片段. onCreateDialog方法将保持不变.

Create a constructor in your TestAlertDialogFragment which will set the listener for us. I am not pasting the whole code of the dialog, only essential fragments. The onCreateDialog method will be as it was.

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);
}

像这样更改您的活动代码:

Change your activity code like this:

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);

}

这篇关于如何在MainActivity上显示带有EditText的自定义alertDialogFragment上插入的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆