首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在MainActivity上显示插入自定义alertDialogFragment上的带有EditText的数字?

如何在MainActivity上显示插入自定义alertDialogFragment上的带有EditText的数字?
EN

Stack Overflow用户
提问于 2016-05-17 04:10:29
回答 1查看 106关注 0票数 1

我使用一个带有editText的自定义editText来插入一个数字,alertdialog正在工作,但是当我单击“正”按钮时,它应该会在main_activity.xml中显示插入的值。

看来我的界面不工作了。我不知道我做错了什么,这是我的代码:

TestAlertDialogFragment.java

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-17 05:07:29

TestAlertDialogListener 您没有在活动中设置侦听器。只要做下面的改变,你就会让它发挥作用。

TestAlertDialogFragment中创建一个构造函数,它将为我们设置侦听器。我并不是在粘贴对话框的全部代码,只是一些基本的片段。onCreateDialog方法将保持原样。

代码语言:javascript
复制
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);
}

更改您的活动代码如下:

代码语言:javascript
复制
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);

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37266958

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档