将值从编辑文本(由用户输入)传递到文本视图

时间:2013-10-25 20:08:55

标签: android

我正在尝试将值从对话框中的编辑文本传递到同一活动的文本视图。 我正在使用2个单独的xml文件进行对话和活动。

运行我的代码时,我收到的错误是无法将编辑文本转换为文本视图: 请让我知道我做错了什么......

我的代码:

public class Water extends Activity implements OnClickListener{

    Button water, date;


    private DatePicker date_picker;
    static final int DATE_DIALOG_ID = 100;
    private static final int DIALOG_TEXT_ENTRY_LOG = 1;

    SimpleDateFormat formatter;
    String result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView (R.layout.diary_water);
        initialize();

        water.setOnClickListener(this);

        final Calendar calendar = Calendar.getInstance();
        formatter = new SimpleDateFormat("EEE, MMM d, yyyy");
        // 3-lettermonth name & 2-char day of month
        date.setText(formatter.format(calendar.getTime()));

    }

    private void initialize() {
        // TODO Auto-generated method stub
        water = (Button) findViewById(R.id.bGlassOfWater);
        date = (Button) findViewById(R.id.bDateWater);

    }

    @SuppressWarnings("deprecation")
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()){

        case R.id.bDateWater:

            break;
        case R.id.bGlassOfWater:
            showDialog (DIALOG_TEXT_ENTRY_LOG);
            break;

        }
    }

        @Override
        public Dialog onCreateDialog(int id) {
            switch (id) {
            case DIALOG_TEXT_ENTRY_LOG:
            LayoutInflater wat = LayoutInflater.from(this);
            final View textEntryViewAcc = wat.inflate(R.layout.glass_of_water, null);
            return new AlertDialog.Builder(Water.this)
                    //.setIconAttribute(android.R.attr.alertDialogIcon)
                    .setTitle(R.string.Water)
                    .setView(textEntryViewAcc)
                    .setPositiveButton(R.string.alert_dialog_save,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int whichButton) {

                                    /* User clicked OK so do some stuff */
                                    LayoutInflater wat = LayoutInflater.from(Water.this);
                                    final EditText glwat= (EditText) ((EditText) dialog).findViewById(R.id.etGlasesOfWater);
                                    result = glwat.getText().toString();
                                    TextView tvwater = (TextView)findViewById(R.id.tvGlassOfWater);
                                    tvwater.setText(result);
                                }


                            })
                    .setNegativeButton(R.string.alert_dialog_cancel,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int whichButton) {

                                    /* User clicked cancel so do some stuff */
                                    dialog.dismiss();
                                }
                            })

                            .create();
        }

        return null;
        }

}

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="wrap_content"
    android:orientation="vertical">

    <EditText
        android:id="@+id/etGlasesOfWater"
        android:layout_width="173dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginRight="27dp"
        android:layout_marginTop="25dp"
        android:layout_toLeftOf="@+id/tvGlasses"
        android:ems="10"
        android:gravity="center_horizontal"
        android:scrollHorizontally="true"
        android:textAppearance="?android:attr/textAppearanceMedium" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/tvGlasses"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/etGlasesOfWater"
        android:layout_alignBottom="@+id/etGlasesOfWater"
        android:layout_alignParentRight="true"
        android:layout_marginRight="19dp"
        android:gravity="left"
        android:text="glasses"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

主要活动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:layout_gravity="top"
    android:background="@drawable/ic_gradient_blue_3"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvTrackIt"
        android:layout_width="wrap_content"
        android:layout_height="40sp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/ic_green_diary"
        android:gravity="center_vertical|left"
        android:paddingLeft="60sp"
        android:text="@string/Track"
        android:textSize="20sp"
        android:textStyle="bold|italic" />

    <Button
        android:id="@+id/bDateWater"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/tvTrackIt"
        android:text="" />

    <Button
        android:id="@+id/bDone"
        android:layout_width="310dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/done_but_gradient"
        android:text="Done" 
        android:textStyle="bold"
        android:layout_marginBottom="4dp"/>

    <TextView
        android:id="@+id/tvGlassOfWater"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/bGlassOfWater"
        android:layout_centerHorizontal="true"
        android:text="" 
        android:textStyle="bold"/>

    <Button
        android:id="@+id/bGlassOfWater"
        android:layout_width="230dp"
        android:layout_height="130dp"
        android:layout_below="@+id/bDateWater"
        android:layout_centerHorizontal="true"
        android:background="@drawable/ic_water_glass"
        android:gravity="top|center_horizontal"
        android:text="Amount of Water"
        android:textStyle="bold|italic" />

</RelativeLayout>

记录结果:

10-25 16:01:07.648: E/AndroidRuntime(5305): FATAL EXCEPTION: main
10-25 16:01:07.648: E/AndroidRuntime(5305): java.lang.ClassCastException: android.app.AlertDialog cannot be cast to android.widget.EditText
10-25 16:01:07.648: E/AndroidRuntime(5305):     at com.margaret.app.easypeasycooking.Water$1.onClick(Water.java:86)
10-25 16:01:07.648: E/AndroidRuntime(5305):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
10-25 16:01:07.648: E/AndroidRuntime(5305):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-25 16:01:07.648: E/AndroidRuntime(5305):     at android.os.Looper.loop(Looper.java:137)
10-25 16:01:07.648: E/AndroidRuntime(5305):     at android.app.ActivityThread.main(ActivityThread.java:5103)
10-25 16:01:07.648: E/AndroidRuntime(5305):     at java.lang.reflect.Method.invokeNative(Native Method)
10-25 16:01:07.648: E/AndroidRuntime(5305):     at java.lang.reflect.Method.invoke(Method.java:525)
10-25 16:01:07.648: E/AndroidRuntime(5305):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-25 16:01:07.648: E/AndroidRuntime(5305):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-25 16:01:07.648: E/AndroidRuntime(5305):     at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:0)

检查您的xml,看起来tvGlassOfWaterEditText,但您尝试在Java代码中强制转换为TextView

TextView tvwater = (TextView)findViewById(R.id.tvGlassOfWater);

如果您的xml文件中有TextView,请发布您的logcat和相关的xml代码。

答案 1 :(得分:0)

解决方案: 不得不在代码的前面定义编辑文本,然后很容易调整正面按钮:

@Override
        public Dialog onCreateDialog(int id) {
            switch (id) {
            case DIALOG_TEXT_ENTRY_LOG:
            LayoutInflater wat = LayoutInflater.from(this);
            final View textEntryViewAcc = wat.inflate(R.layout.glass_of_water, null);
            final EditText glwater = (EditText) textEntryViewAcc.findViewById(R.id.etGlasesOfWater);
            return new AlertDialog.Builder(Water.this)
                    //.setIconAttribute(android.R.attr.alertDialogIcon)
                    .setTitle(R.string.Water)
                    .setView(textEntryViewAcc)
                    .setPositiveButton(R.string.alert_dialog_save,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int whichButton) {

                                    String result = glwater.getText().toString();
                                    TextView tvwater = (TextView)findViewById(R.id.tvGlassOfWater);
                                    tvwater.setText(result);

感谢@codeMagic寻求帮助:)

答案 2 :(得分:0)

因为您向Dialog投了EditText。请尝试这种方式。

final EditText edit = (EditText)dialog.findViewById(R.id.editText);
final TextView text = (TextView)dialog.findViewById(R.id.textView);