【问题标题】:Android dialog - changing dataAndroid 对话框 - 更改数据
【发布时间】:2013-07-02 14:13:48
【问题描述】:

我有 MainActivity 我有一个打开对话框的按钮。单击此按钮后,我的对话框框架打开了,现在此对话框中有两个文本字段和两个按钮。

如何通过单击其中一个按钮来更改该字段中的文本?

我尝试了一些技巧,但我总是得到NullPointerException

提前致谢!

【问题讨论】:

    标签: android layout view android-activity dialog


    【解决方案1】:

    我会使用自定义对话框。

    您需要使用对话框对象来查找ViewById 并初始化视图。

    dialog.xml

       <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    
        android:orientation="vertical" >
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/button1"
            android:layout_alignBottom="@+id/button1"
            android:layout_alignParentRight="true"
            android:layout_marginRight="58dp"
            android:text="Cancel" />
    
        <EditText
            android:id="@+id/ed1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView1"
            android:layout_marginTop="56dp"
            android:layout_toLeftOf="@+id/button2"
              />
    
        <EditText
              android:id="@+id/ed2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView2"
            android:layout_alignParentTop="true"
            android:layout_marginTop="16dp"
            />
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/textView2"
            android:layout_below="@+id/textView2"
            android:layout_marginRight="27dp"
            android:layout_marginTop="39dp"
            android:text="ChangeText" />
    
    </RelativeLayout>
    

    然后在你的活动中说点击按钮

     dialogPopup();
    

    然后

    public void dialogPopup()
    {
        final Dialog d = new Dialog(MainActivity.this); // initialize dialog
        d.setContentView( R.layout.dialog);
        d.setTitle("my title");
        final EdiText ed1= (TextView) d.findViewById(R.id.ed1);
        // initialize edittext using the dialog object. 
        final EdiText ed2= (TextView) d.findViewById(R.id.ed2);
        Button b = (Button) d.findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() // button 1 click
        {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                        // on click of button 1 change the text in textview's
                ed1.setText("Changing text 1");
                ed2.setText("Changing text 2");
            }
    
        });
         Button b1 = (Button) d.findViewById(R.id.button2); // button2 click
            b1.setOnClickListener(new OnClickListener()
            {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    d.dismiss(); // dismiss dialog
                }
    
            });
            d.show(); // show the dialog
    }
    

    【讨论】:

      【解决方案2】:

      使用 AlertDialog.Builder 创建对话框时,设置 Button 的 onClickListener 以更改 EditText 的值:

      AlertDialog.Builder dialog = new AlertDialog.Builer(this);
      ...
      final EditText edit = new EditText(this);
      dialog.setView(edit);
      ...
      dialog.setPositiveButton("Change the freakin text",new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog,int id){
                          edit.setText("bla bla bla");
                      }
                    });
      ...
      

      【讨论】:

      • 但是我已经创建了带有随机文本的 EditText 字段,我想通过单击一个按钮来更改它。如何获取放置在对话框布局中的此 EditText 字段的 ID?
      • 您已经拥有 EditText 的引用,因为您在将其添加到对话框之前以编程方式创建了它。如果您没有以编程方式创建它,而是在 XML 布局中创建,则使用 findViewById(id) 其中 id 是其在布局中的名称。
      • @dejvid 你需要使用对话框对象来初始化editext。您可以将当前视图层次结构的findViewById 设置为活动。在您的情况下,您有对话框。所以你需要使用对话框对象来初始化edittext。
      • @dejvid 检查我的编辑。它应该工作。根据您的需要修改
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-05
      • 2012-11-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多