0

我的项目我的屏幕上有一个文本视图和按钮。在按钮单击事件上,我显示自定义对话框(对话框内容编辑文本和按钮)。我将一些数据放在对话框编辑文本中,然后单击对话框保存按钮。
现在我想在单击按钮时在屏幕文本视图上显示对话框编辑文本的值。我该怎么做.... 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();
            }
        });

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

4

2 回答 2

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();
于 2012-09-27T11:27:46.107 回答
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();
                }
            });
        }
     }
于 2012-09-27T11:34:17.037 回答