2

我在一个 xml 中有两个 textView,即。客户登录和供应商登录...当我点击客户登录或供应商登录弹出窗口应该打开我想显示我的登录表单。请帮帮我。

这是我的登录表单:login_layout.xml

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background"
    android:orientation="vertical" >

    <include layout="@layout/header_layout" />
        <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_margin="10dip"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/lbl_username"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dip"
            android:gravity="center"
            android:textColor="#000000"
            android:text="@string/username"
            />

        <EditText
            android:id="@+id/et_username"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dip"
            android:hint="@string/enter_username"
            android:inputType="textEmailAddress" />

        <TextView
            android:id="@+id/lbl_password"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dip"
            android:gravity="center"
             android:textColor="#000000"
            android:text="@string/password"
            />

        <EditText
            android:id="@+id/et_password"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dip"
            android:hint="@string/enter_password"
            android:inputType="textPassword"
            android:singleLine="true" />
         <TextView
            android:id="@+id/lbl_forgot"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="25dip"
            android:gravity="center"
            android:textColor="#000000"
            android:text="@string/forgot_password"
            android:textSize="18dp"
            />

        <Button
            android:id="@+id/loginButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="15dip"
            android:text="@string/login"
             />
    </LinearLayout>

</LinearLayout>

这是 LoginActivity.java:

package com.Login;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


import org.json.JSONException;
import org.json.JSONObject;


public class LoginActivity extends Activity {

    private EditText etUsername, etPassword;
    private Button loginButton;
    private JSONParser jsonParser;
    private static String loginURL = "http://www.xyz.com?login.php";

    private Bundle bundle;
    private String success;
    /** The dialog. */
    private ProgressDialog dialog;



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

        TextView textv = (TextView) findViewById(R.id.lbl_forgot);
        textv.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent();
                intent.setClass(v.getContext(), ForgotPassActivity.class);
                startActivity(intent);

            }
        });
        intitDisplay();
        addListeners();

    }

    private void addListeners() {
        loginButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                if (etUsername.getText().length() == 0)
                    etUsername.setError("please enter username");

                if (etPassword.getText().length() == 0)
                    etPassword.setError("please enter password");

                if ((etUsername.getText().length() > 0)
                        && (etPassword.getText().length() > 0)) {
                    jsonParser = new JSONParser();

                    if (jsonParser.isNetworkAvailable(LoginActivity.this)) {
                        new BackgroundTask().execute();
                    } else {
                        Toast.makeText(LoginActivity.this,
                                "Network not available", Toast.LENGTH_LONG)
                                .show();
                    }
                }

            }
        });

    }

    private void intitDisplay() {
        etUsername = (EditText) findViewById(R.id.et_username);
        etPassword = (EditText) findViewById(R.id.et_password);
        loginButton = (Button) findViewById(R.id.loginButton);
        bundle = new Bundle();
        dialog = new ProgressDialog(LoginActivity.this);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.login_layout, menu);
        return true;
    }

    /** calling web service to get creditors list */
    /**
     * The Class BackgroundTask.
     */
    private class BackgroundTask extends AsyncTask<String, Void, String> {

        /** The resp. */
        String resp;

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPreExecute()
         */
        protected void onPreExecute() {

            dialog.setMessage("Loading...");
            dialog.setCancelable(false);
            dialog.show();

        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#doInBackground(Params[])
         */
        protected String doInBackground(final String... args) {

            Log.v("ENTERED USERNAME::", "" + etUsername.getText().toString());
            String newUrl = loginURL + "username="
                    + etUsername.getText().toString() + "&password="
                    + etPassword.getText().toString();
            JSONObject json = jsonParser.getJSONFromUrl(newUrl);

            try {
                success = json.getString("msg");
                Log.v("SUCCESS:: ", success);
                if (success.trim().toString().equalsIgnoreCase("SUCCESS")) {
                    String loggedUsername = json.getString("USER_NAME");
                    bundle.putString("loggedUser", loggedUsername);
                    Log.v("Logged User::", loggedUsername);

                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;

        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
         */
        protected void onPostExecute(String list) {
            dialog.dismiss();
            if (success.trim().toString().equalsIgnoreCase("SUCCESS")) {
                Toast.makeText(LoginActivity.this, "Login Succesfull..",
                        Toast.LENGTH_LONG).show();
                Intent intent = new Intent(LoginActivity.this,
                        HomeActivity.class).putExtras(bundle);
                startActivity(intent);
            } else {
                Toast.makeText(LoginActivity.this, "Login failed...",
                        Toast.LENGTH_LONG).show();
            }

        }
    }
}
4

4 回答 4

15

用这个

创建一个新的 xml 文件,其中包含文本字段、编辑文本、按钮,即您想要在弹出窗口中显示的登录表单。现在在您想要显示弹出窗口的地方调用以下方法。

private void callLoginDialog() 
     {
         myDialog = new Dialog(this);
         myDialog.setContentView(R.layout.yourxmlfileID);
         myDialog.setCancelable(false);
         Button login = (Button) myDialog.findViewById(R.id.yourloginbtnID);

          emailaddr = (EditText) myDialog.findViewById(R.id.youremailID);
          password = (EditText) myDialog.findViewById(R.id.yourpasswordID);
          myDialog.show();

         login.setOnClickListener(new OnClickListener()
         {

            @Override
            public void onClick(View v)
            {
                    //your login calculation goes here
            }
        });


     }

让我知道它是否对你有帮助......

于 2012-09-13T09:25:32.970 回答
2

适用@android:style/Theme.Dialog于您的登录活动。您可以在清单编辑器中执行此操作。

于 2012-09-13T09:16:36.900 回答
2

在你的清单文件中给出这样的

<activity android:name=".LoginActivity"
              android:label="Login"
              android:theme="@style/CustomDialogTheme"
              android:configChanges="locale|keyboardHidden|orientation"/>

并在 styles.xml 中将 customDialogTheme 指定为

   <style name="CustomDialogTheme" parent="@android:style/Theme.Dialog">
    <item name="android:bottomBright">#fff</item>
    <item name="android:bottomDark">#fff</item>
    <item name="android:bottomMedium">#fff</item>
    <item name="android:centerBright">#fff</item>
    <item name="android:centerDark">#fff</item>
    <item name="android:centerMedium">#fff</item>
    <item name="android:fullBright">#7D266A</item>
    <item name="android:fullDark">#7D266A</item>
    <item name="android:topBright">#2f6699</item>
    <item name="android:topDark"#2f6699</item>
</style>
于 2012-09-13T09:17:35.580 回答
1

使用自定义对话框。

为客户供应商创建布局

根据您的要求在单击时显示自定义对话框对话框。

Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.customerlogin);

或者

dialog.setContentView(R.layout.supplierlogin);

dialog.show();
于 2012-09-13T09:18:17.370 回答