简体   繁体   中英

Android: StoredPreferences + WebView Link

I'm trying to store the users input from a text field in shared preferences and use that input in a webview link.

Here's what I have so far;

LoginActivity.java

package com.example.app;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.content.Intent;

public class LoginActivity extends Activity {

EditText subdomain;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    subdomain = (EditText) findViewById(R.id.subdomain);

    Button btn=(Button)findViewById(R.id.sign_in_button);

    btn.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            Intent myIntent = new Intent(LoginActivity.this, 
MainActivity.class);
            LoginActivity.this.startActivity(myIntent);
        }
    });
}

public void saveInfo (View view) {
    SharedPreferences sharedPref = getSharedPreferences("spfile", 
Activity.MODE_PRIVATE);

    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString("name", YourSchool.getText().toString());
    editor.commit();
}
}

MainActivity.java

package com.example.app;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

private WebView mWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mWebView = (WebView) findViewById(R.id.activity_main_webview);

    // Force links and redirects to open in the WebView instead of in a 
browser
    mWebView.setWebViewClient(new WebViewClient());

    // Enable Javascript
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    // Use remote resource

mWebView.loadUrl("https://"+client_subdomain+".domain.co.uk/texts");

    // Stop local links and redirects from opening in browser instead 
of WebView
    mWebView.setWebViewClient(new MyAppWebViewClient());


}
public void displayData (View view) {
    SharedPreferences sharedPref = getSharedPreferences("spfile", 
Activity.MODE_PRIVATE);
    String client_subdomain = sharedPref.getString("name", "");
}

// Prevent the back-button from closing the app
@Override
public void onBackPressed() {
    if(mWebView.canGoBack()) {
        mWebView.goBack();
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is 
present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

activity_login.xml

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.app.LoginActivity">

        <LinearLayout
        android:id="@+id/email_login_form"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <EditText
            android:id="@+id/YourSchool"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Your school"
            android:maxLines="1"
            android:singleLine="true" />

        <Button
            android:id="@+id/sign_in_button"
            style="?android:textAppearanceSmall"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:onClick="webView"
            android:text="SIGN IN"
            android:textStyle="bold" />

    </LinearLayout>
</ScrollView>
</LinearLayout>

activity_main.xml

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<WebView
    android:id="@+id/activity_main_webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app">

<uses-permission android:name="android.permission.INTERNET" />

<!-- To auto-complete the email text field in the login form with the 
user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"></activity>
    <activity
        android:name=".LoginActivity"
        android:label="@string/title_activity_login">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" 
/>
        </intent-filter>
    </activity>
</application>

</manifest>

My problem is that the client_subdomain is flagged as red in the MainActivity.java and when I try to build the project, I get the error: cannot find symbol variable client_subdomain

I think it's probably something small that I've missed out but any help would be greatly appreciated.

Many thanks, Sam

Its because haven't declared "client_subdomain" as a variable within scope of onCreate

why don't you try changing displayData to

public String displayData () {
    SharedPreferences sharedPref = getSharedPreferences("spfile", 
    Activity.MODE_PRIVATE);
    return sharedPref.getString("name", "");
}

and in onCreate of the Main Activity

...
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

// Use remote resource
mWebView.loadUrl("https://"+displayData()+".domain.co.uk/texts");
...

Also, I don't see you ever calling "saveInfo" anywhere in your Login Activity. As well, you should probably change the function too. You aren't grabbing the text from the EditText correctly (whatever YourSchool) is

public void saveInfo() {
    SharedPreferences sharedPref = getSharedPreferences("spfile", Activity.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString("name", subdomain.getText().toString());
    editor.commit();
}

And this will be what your onCreate should have,

protected void onCreate(Bundle savedInstanceState) {
    ...
    btn.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            saveInfo();
            Intent myIntent = new Intent(LoginActivity.this, MainActivity.class);
            LoginActivity.this.startActivity(myIntent);
        }
    });
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM