2017-04-21 44 views
0

我試圖從共享首選項中的文本字段中存儲用戶輸入並在webview鏈接中使用該輸入。Android:StoredPreferences + WebView鏈接

這是我到目前爲止;

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> 

我的問題是,client_subdomain在MainActivity.java標記爲紅色,當我嘗試生成項目,我得到錯誤:無法找到符號變量client_subdomain

我認爲這可能是一個小的,我錯過了,但任何幫助將不勝感激。

非常感謝, 山姆

+0

'client_subdomain'是一個方法'的局部變量displayData ()',你不能在其範圍之外引用它。將'client_subdomain'設置爲類字段或使該函數返回'client_subdomain'。 –

回答

0

它,因爲還沒有宣佈 「client_subdomain」 爲的onCreate

爲什麼範圍內的變量不試試改變displayData到

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

和在主要活動的創建

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

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

另外,我沒有看到您曾經在您的登錄活動的任何地方調用過「saveInfo」。同樣,你也應該改變這個功能。你是不是正確抓住從的EditText文本(無論YourSchool)是

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

,這將是你的onCreate應該有什麼,

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); 
     } 
    }); 
    ... 
} 
+0

謝謝你,看起來像現在幾乎那裏:)你是對的saveInfo部分。我假設這個動作需要發生onButtonClick爲sign_in_button,但我不知道如何正確調用它。 – sampearson

+0

@sampearson我編輯了我的答案來回答你的問題。看看它的底部。 – phazedlite