When the user opens the appli开发者_JS百科cation, there is a screen with a button on it, which says "login."
The user clicks on the button, and a webview pops up to allow him to log in to the website.
After logging in (the app would need to know somehow), the webview would disappear, and then a list of usernames will pop up. (ListView?)
When the user clicks on one of the usernames, a webview of the username's profile will pop up. Of course, when the user pushes "back", it goes back to the list of usernames.
Can someone explain this to me in terms of Activity and Views? Am I using two activities to do this? Do I hide webview or listview when the user clicks between them?
I did the tutorial (notepad tutorial), but I'm still confused as to what is the best way to develop this.
Thanks
When the user opens the application, there is a screen with a button on it, which says "login." The user clicks on the button, and a webview pops up to allow him to log in to the website. After logging in (the app would need to know somehow),
Yo can do this with two separate Activity
classes. I would put the WebView
in its own Activity
. This is easier than managing lots of different View
objects yourself. Also, you'll get transitions between different things if you put each part in its own Activity
.
You'll can launch the login Activity
with the startActivityForResult()
method, allowing it to return if the login was successful or not.
If you want to detect the login, you can monitor events in a WebView
using a WebViewClient
. You set the WebViewClient
of your WebView
using the setWebViewClient()
method.
the webview would disappear,
Simply start the next Activity
using an Intent
and call the finish()
method on your first Activity
. If you do this then the use won't come back to login button Activity if they click back as it won't be on the stack any more.
What I'm not clear on is how long the login at the website will be valid for. You may need to set the flags on the Activities in your Manifest, to ensure the user has to log in again if they leave and then return to your application.
and then a list of usernames will pop up. (ListView?)
Use a ListActivity
. This is an Activity
which comes with the API designed for displaying a single ListView
.
When the user clicks on one of the usernames, a webview of the username's profile will pop up. Of course, when the user pushes "back", it goes back to the list of usernames.
So use the onListItemClick()
method in ListActivity
to detect the touch and launch a new Activity
containing the WebView
to show the profile. As this is in a new Activity
the back handling is all automatic.
Essentially it could all be done within one activity. I can try to easily demonstrate how it could be done.
- When you run your app,
Activity
(AndroidManifest.xml) is called. View
(could be called, main.xml) is shown displaying a login button- Upon clicking the button, you launch your
WebView
- Successful login, switches the
View
to a custom view displaying usernames on aListView
(might be a predefined XML file you create named userlist.xml) - Using
onClick
on a specific username will launch aWebView
regarding that specific username.
To return back to your application when the user hits the Back button, you might need to utilize onPause
and onResume
but I am not sure.
This may not be the best approach, or even a good one, but it might help clear up any confusion.
EDIT:
An Activity
is what gets bound to the AndroidManifest.xml as the main entry point into an application. A View
contains user interactive components within your Activity
, such as a login button, displaying username's, viewing contents on the web (WebView
), etc.
First of all I'd consider a different way of approaching this problem.
You could create your own login layout in Andoid and send your login data to the website. After doing this you should create a ListView of all user names. If the user selects one of these user names you should open a WebView.
The problem would be, that you would have to check whether the login was successful or not! Basically you need to do a HttpRequest, parse the output and check it.
If it's your website you want to login you could write a small wrapper for your login which returns true/false for your login. (php wrapper which returns xml or plaintext)
Anyway, if you really want to do the login via WebView you can check it via:
private void initWebView(final WebView mWebView) {
mWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Page = new WebView(YourClass.this);
Page.getSettings().setJavaScriptEnabled(true);
Page.loadUrl(url);
if(url.contains("OkDoLogIn.php")) {
/* Check if login was successful if true start new Activity */
}
return true;
}
});
}
With url.contains("OkDoLogIn.php") you basically check which url is about to open. In your case it would be the url or a part of the url of your login button.
Nevertheless you would have to check if your login was successful or not!
Edit: See 1st comment!
精彩评论