定位问题

时间:2012-09-04 05:03:04

标签: android

我正在尝试在一个活动中调用一个对话框。无论什么时候,我改变了自动旋转,我已经使它成为风景,然后在回到肖像时它导航到之前的活动。请帮我一样。请找到对话框的代码。

public class TransSuccessfullyDialog extends Dialog implements OnClickListener
    {

        public  TransSuccessfullyDialog(Context context)
        {

            super(context);

            requestWindowFeature(Window.FEATURE_NO_TITLE);

            if(statusString.equals("1")){

                setContentView(R.layout.transaction_complete);
                //@Override


                ImageView img = (ImageView) findViewById(R.id.imageView1);    
                //Bitmap src = null;

                Bitmap bmp = BitmapFactory.decodeFile("data/data/com.android.epaisa/logo.jpg");
                Bitmap resizedBitmap=Bitmap.createScaledBitmap(bmp, 50, 50, true);
                img.setImageBitmap(resizedBitmap);


                refNo=(TextView)findViewById(R.id.textView1);
                refNo.setText("Thank you!");

                                card=(TextView)findViewById(R.id.textView3);
                card.setText(" Number "+ RefNo);



                actfail_btn=(Button) findViewById(R.id.addfromcontactsbtn);
                actfail_btn.setOnClickListener(this);

            }

        }
        public void onBackPressed() {
            // Do as you please
        }
        public void onClick(View v) 
        {
            if(v==actfail_btn)
            {
startActivity(intent);

            }
        }

    }

XML文件

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/background" >

    <Button
        android:id="@+id/addfromcontactsbtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:text="  Ok  "
        android:textSize="22sp"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/addfromcontactsbtn"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="75dp"
        android:background="@drawable/success"
        android:gravity="left" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Success"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="175dp"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/addfromcontactsbtn"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="20dp" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

这是MessageBox的示例。使用此选项或将其与您自己的对话框进行比较并更正您的问题:

import java.util.ArrayList;

import org.mabna.order.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MessageBox {

    static ArrayList<AlertDialog> arrAlertDialog = new ArrayList<AlertDialog>();
    static OnMyDialogResult mDialogResult; // the callback

    public enum MessageBoxState {
        OK, YesNo
    }

    public enum MessageBoxResult {
        OK, Cancel, Yes, No
    }

    public final static void show(final Activity parent, String title,
            String message, MessageBoxState state) {

        // if (alertDialog != null)
        // {
        // alertDialog.dismiss();
        // }

        AlertDialog.Builder builder;

        LayoutInflater inflater = (LayoutInflater) parent
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.dialog_messagebox,
                (ViewGroup) parent.findViewById(R.id.layout_root));
        layout.setMinimumWidth(400);



        TextView tvTitle = (TextView) layout.findViewById(R.id.tvTitle);
        tvTitle.setText(Farsi.Convert(title));

        tvTitle.setPadding(10, 10, 10, 10);

        TextView tvMessage = (TextView) layout.findViewById(R.id.tvMessage);
        tvMessage.setText(message);

        tvMessage.setPadding(10, 10, 10, 10);

        Button btnOK = (Button) layout.findViewById(R.id.btnOK);
        Button btnYes = (Button) layout.findViewById(R.id.btnYes);
        Button btnNo = (Button) layout.findViewById(R.id.btnNo);
        Button btnCancel = (Button) layout.findViewById(R.id.btnCancel);
        btnOK.setText("OK");

        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) btnOK
                .getLayoutParams();
        lp.setMargins(10, 10, 10, 10);
        btnOK.setLayoutParams(lp);

        btnYes.setText("Yes");

        lp = (LinearLayout.LayoutParams) btnYes
                .getLayoutParams();
        lp.setMargins(10, 10, 10, 10);
        btnYes.setLayoutParams(lp);

        btnNo.setText("No");

        btnNo.setPadding(10, 10, 10, 10);
        lp = (LinearLayout.LayoutParams) btnNo
                .getLayoutParams();
        lp.setMargins(10, 10, 10, 10);
        btnNo.setLayoutParams(lp);

        btnCancel.setText("Cancel");

        btnCancel.setPadding(10, 10, 10, 10);
        lp = (LinearLayout.LayoutParams) btnCancel
                .getLayoutParams();
        lp.setMargins(10, 10, 10, 10);
        btnCancel.setLayoutParams(lp);

        btnOK.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                btnOK_onClick();
            }
        });
        btnYes.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                btnYes_onClick();
            }
        });
        btnNo.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                btnNo_onClick();
            }
        });
        btnCancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                btnCancel_onClick();
            }
        });

        LinearLayout layout_OK = (LinearLayout) layout
                .findViewById(R.id.layout_OK);
        LinearLayout layout_YesNo = (LinearLayout) layout
                .findViewById(R.id.layout_YesNo);

        switch (state) {
        case OK:
            layout_YesNo.setVisibility(LinearLayout.GONE);
            layout_OK.setVisibility(LinearLayout.VISIBLE);
            break;
        case YesNo:
            layout_YesNo.setVisibility(LinearLayout.VISIBLE);
            layout_OK.setVisibility(LinearLayout.GONE);
            break;
        default:
            break;
        }

        builder = new AlertDialog.Builder(parent);
        builder.setView(layout);

        AlertDialog alertDialog;

        alertDialog = builder.create();
        alertDialog.setCancelable(false);
        alertDialog.show();
        mDialogResult = null; // causes each messageBox instance has its own
                                // DialogResult because it removes previous
                                // DialogResults

        arrAlertDialog.add(alertDialog);
        return;
    }


    private static AlertDialog getLastDialog() {
        if (arrAlertDialog.size() > 0)
        {
            AlertDialog dialog = arrAlertDialog.get(arrAlertDialog.size() - 1);
            arrAlertDialog.remove(dialog);
            return dialog;
        }
        return null;
    }

    private final static void btnOK_onClick() {
        AlertDialog currentdialog = getLastDialog();
        if (mDialogResult != null) {
            mDialogResult.finish(MessageBoxResult.OK);
            if (currentdialog != null)
                currentdialog.dismiss();
            mDialogResult.dialogClosed();
        } else {
            if (currentdialog != null)
                currentdialog.dismiss();
        }
    }

    private final static void btnYes_onClick() {
        AlertDialog currentdialog = getLastDialog();
        if (mDialogResult != null) {
            mDialogResult.finish(MessageBoxResult.Yes);
            if (currentdialog != null)
                currentdialog.dismiss();
            mDialogResult.dialogClosed();
        } else {
            if (currentdialog != null)
                currentdialog.dismiss();
        }
    }

    private final static void btnNo_onClick() {
        AlertDialog currentdialog = getLastDialog();
        if (mDialogResult != null) {
            mDialogResult.finish(MessageBoxResult.No);
            if (currentdialog != null)
                currentdialog.dismiss();
            mDialogResult.dialogClosed();
        } else {
            if (currentdialog != null)
                currentdialog.dismiss();
        }
    }

    private final static void btnCancel_onClick() {
        AlertDialog currentdialog = getLastDialog();
        if (mDialogResult != null) {
            mDialogResult.finish(MessageBoxResult.Cancel);
            if (currentdialog != null)
                currentdialog.dismiss();
            mDialogResult.dialogClosed();
        } else {
            if (currentdialog != null)
                currentdialog.dismiss();
        }
    }

    public final static void setDialogResult(OnMyDialogResult dialogResult) {
        mDialogResult = dialogResult;
    }

    public interface OnMyDialogResult {
        void finish(MessageBoxResult result);

        void dialogClosed();
    }

}

它的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_root"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:orientation="vertical"
    android:padding="10dp" >

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_weight="1"
        android:text="TextView"
        android:textStyle="bold" >
    </TextView>

    <TextView
        android:id="@+id/tvLine"
        android:layout_width="fill_parent"
        android:layout_height="2dip"
        android:layout_weight="1"
        android:background="@color/black"
        android:text="" >
    </TextView>

    <TextView
        android:id="@+id/tvMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_weight="1"
        android:text="TextView" >
    </TextView>

    <LinearLayout
        android:id="@+id/layout_YesNo"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center_horizontal"
        android:gravity="center" >

        <Button
            android:id="@+id/btnCancel"
            android:layout_width="70dip"
            android:layout_height="40dip"
            android:text="Button" >
        </Button>

        <Button
            android:id="@+id/btnNo"
            android:layout_width="70dip"
            android:layout_height="40dip"
            android:text="Button" />

        <Button
            android:id="@+id/btnYes"
            android:layout_width="70dip"
            android:layout_height="40dip"
            android:text="Button" >
        </Button>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout_OK"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:baselineAligned="true"
        android:orientation="vertical" >

        <Button
            android:id="@+id/btnOK"
            android:layout_width="80dip"
            android:layout_height="40dip"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="Button" >
        </Button>
    </LinearLayout>

</LinearLayout>

答案 1 :(得分:-2)

您只需在清单中定义android:screenOrientation属性,如下所示。

<activity android:name=".YourActivity" android:screenOrientation="landscape"/>.