【问题标题】:how to set a text to a Edittext which is in a dialog box?如何将文本设置为对话框中的 Edittext?
【发布时间】:2011-09-13 12:06:48
【问题描述】:

问题是如你所见,我无法在 showDialog() 之前动态设置 edittext 的值。

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
 import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends Activity {
private static final int DIALOG_TEXT_ENTRY = 7;

private int user_id;
private int dialogChoice;
private String mobileNum;
private EditText input2 ;
private EditText input1 ;
public TextView textView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.alert_dialog_text_entry);
    TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
     mobileNum =tm.getLine1Number();
    // input1.setText(mobileNum);   
     textView.setText("hello");
    showDialog(DIALOG_TEXT_ENTRY);

}
@Override
 protected Dialog onCreateDialog(int i) {


    switch (i) {

    case DIALOG_TEXT_ENTRY:
            LayoutInflater factory = LayoutInflater.from(this);
            final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
            input2 = (EditText) textEntryView.findViewById(R.id.number_edit);
            input1 = (EditText) textEntryView.findViewById(R.id.username_edit);
            textView = (TextView) textEntryView.findViewById(R.id.username_view);
            return new AlertDialog.Builder(MainActivity.this)
               // .setIconAttribute(android.R.attr.accountType)
                .setTitle(R.string.alert_dialog_text_entry)
                .setView(textEntryView)
                .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                          Intent i = new Intent(MainActivity.this, IccActivity.class);
                          startActivity(i);
                        /* User clicked OK so do some stuff */
                    }
                })
                .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked cancel so do some stuff */
                    }
                })
                .create();
    }
    return null;
 }

}

textView.setText("hello"); this line kills the app.

提前致谢。

【问题讨论】:

    标签: android dialog android-edittext


    【解决方案1】:

    您应该将这一行 textView.setText("hello"); 放入 onCreateDialog() 方法中,因为您要在初始化之前设置值。

    【讨论】:

    • 谢谢。有用。我之前试图分配一个值以获得对它的引用。
    • 现在您应该接受答案,因为它会像未回答一样流动
    【解决方案2】:

    你还没有初始化 textView。在执行 textView.setText()

    之前,您需要在 onCreate() 中使用它

    textView = (TextView) textEntryView.findViewById(R.id.username_view);

    【讨论】:

      【解决方案3】:

      我猜你得到的是 NullPointerException?您应该首先提供您的“textView”字段。

      【讨论】:

        【解决方案4】:

        找到对话框中的 TexView 文本并设置 TextView 的文本 试试这个link

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-07-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多