从android中的自定义对话框edittext获取价值

时间:2012-09-27 11:24:27

标签: android-emulator

我的项目我的屏幕上有一个文本视图和按钮。在按钮单击事件上,我显示自定义对话框(对话框内容编辑文本和按钮)。我在对话框编辑文本中添加了一些数据,然后单击对话框保存按钮 现在我想在按钮点击时在屏幕文本视图上显示对话框编辑文本的值。我怎样才能做到这一点.... holder.list1.setOnClickListener(new OnClickListener(){

            public void onClick(View v) {
                final TextView tv=(TextView)v.findViewById(R.id.textView3);
                final Dialog dialog = new Dialog(activity);
                dialog.setContentView(R.layout.customdailogfroaddmilk);
                dialog.setTitle("Account");

                // set the custom dialog components - text, image and button
                final EditText text = (EditText) dialog.findViewById(R.id.editText1);
                text.setText(tv.getText().toString());


                Button save = (Button) dialog.findViewById(R.id.button1);
                Button cancle = (Button) dialog.findViewById(R.id.button2);
                save.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {

                        dialog.dismiss();
                        tv.setText(text.getText().toString());
                    }
                });

                cancle.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });

                dialog.show();
            }
        });

按钮(保存)单击事件edittext(保存)值未在textview(tv)中设置 ????? .....

2 个答案:

答案 0 :(得分:2)

在布局文件夹中创建布局,并将其作为“查看对话框对象”提供。 对于下面显示的示例,您需要在该布局中使用TextView,Button和EditText。

        final Dialog dialog = new Dialog(context,
                        android.R.style.Theme_Translucent_NoTitleBar);
        Window window = dialog.getWindow();
        window.setLayout(ViewGroup.LayoutParams.FILL_PARENT,
                        ViewGroup.LayoutParams.FILL_PARENT);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(R.layout.xmlfile);

        Button ok = (Button) dialog.findViewById(R.id.alert_ok_button);
        TextView  alert_title = (TextView ) dialog.findViewById(R.id.alert_title);
        final EditText shelf_name_edit=(EditText)dialog.findViewById(R.id.shelf_name_edit_area);

        alert_title.setText(title);
        alert_title.setTextSize(20);
        ok.setText("OK");
        ok.setTextSize(20);

        ok.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                dialog.cancel();
                Log.i("EditText Value",shelf_name_edit.getEditableText().toString());
            }

        });
        dialog.show();

答案 1 :(得分:1)

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/ClkBtn"
android:text="Click Me"></Button>
</RelativeLayout>

mydialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:background="#ffffff" android:orientation="vertical"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"

 >


 <TextView android:id="@+id/Tv1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:textSize="15px"
 android:textColor="#6aa4cc"
 android:text="Friendcaster for Facebook"
 android:padding="10dip"
 />



 <TextView android:id="@+id/Tv2"
 android:layout_width="wrap_content" android:layout_height="wrap_content"
 android:textSize="14px"
 android:textColor="@android:color/black"
 android:layout_below="@+id/Tv1"
 android:textStyle="bold"
 android:text="A , B , C , D and or 5 friend like your status "
 android:padding="10dip"
 />


 <View  android:layout_height="15dip" android:layout_width="fill_parent"/>
 <LinearLayout android:layout_height="wrap_content" android:layout_width="fill_parent"
     android:orientation="horizontal" android:background="#d6d6d6"
     android:layout_weight="3"
      >
 <LinearLayout android:layout_height="wrap_content" android:layout_width="wrap_content"
     android:orientation="vertical" 
     android:gravity="left"
     android:id="@+id/ll"
     android:layout_weight="1" android:padding="5dip"
      >
        <ImageView android:src="@drawable/video"
                android:layout_height="30dip" android:layout_width="30dip"  android:layout_gravity="center_vertical|center_horizontal"
             /> 
             <TextView android:layout_height="wrap_content" android:layout_width="wrap_content"
                 android:text="view" android:layout_gravity="center_vertical|center_horizontal"
                   android:textColor="#0388e5"
                  />

     </LinearLayout>
     <View  android:layout_height="match_parent" android:layout_width="1dip" android:background="@android:color/darker_gray"/>

 <LinearLayout android:layout_height="wrap_content" android:layout_width="wrap_content"
     android:orientation="vertical"
     android:gravity="center"
     android:layout_weight="1" android:padding="5dip"
      >
        <ImageView android:src="@drawable/chat"
            android:layout_height="30dip" android:layout_width="30dip" android:layout_gravity="center_vertical|center_horizontal"
             /> 
             <TextView android:layout_height="wrap_content" android:layout_width="wrap_content"
                 android:text="Reply" android:layout_gravity="center_vertical|center_horizontal"

                 android:textColor="#0388e5"
                  />

     </LinearLayout>
 <View  android:layout_height="match_parent" android:layout_width="1dip" android:background="@android:color/darker_gray"/>    

 <LinearLayout android:layout_height="wrap_content" android:layout_width="wrap_content"
     android:orientation="vertical"
     android:layout_weight="1" android:padding="5dip"

     android:gravity="right"
      >
        <ImageView android:src="@drawable/cancel"
              android:layout_height="30dip" android:layout_width="30dip"  android:layout_gravity="center_vertical|center_horizontal"
             /> 
             <TextView android:layout_height="wrap_content" android:layout_width="wrap_content"
                 android:text="Zip" android:layout_gravity="center_vertical|center_horizontal"
                   android:textColor="#0388e5"
                  />

     </LinearLayout>

</LinearLayout>



</LinearLayout>

main.java

package com.example.cusmizepopup;

import android.os.Bundle;
import android.os.Message;
import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;


public class MainActivity extends Activity {

        Dialog myDialog;
        Button myButton;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            myButton = (Button) findViewById(R.id.ClkBtn);

            myButton.setOnClickListener(new OnClickListener() {          
            @Override
                public void onClick(View v) 
            {
                    myDialog = new Dialog(MainActivity.this);
                    myDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    myDialog.setContentView(R.layout.mydialog);
                    //myDialog.setTitle("My Dialog");

                    myDialog.setCancelable(true);

                    //myDialog.setCancelable(true);
                    //myDialog.setCancelMessage("Are you Sure ? ")
                    LinearLayout button = (LinearLayout) myDialog.findViewById(R.id.ll);
                    button.setOnClickListener(new OnClickListener() {
                    @Override
                        public void onClick(View v) 
                        {
                        //myDialog.dismiss();
                        Toast.makeText(getApplicationContext(), "hi", Toast.LENGTH_LONG).show();
                        }
                    });

                    myDialog.unregisterForContextMenu(myButton);
                    myDialog.show();
                }
            });
        }
     }