我在一个 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();
}
}
}
}