2017-04-19 54 views
-3

我試圖將共享首選項中的文本字段的用戶輸入存儲並在webview鏈接中使用該輸入。Android的存儲變量和在web視圖中使用鏈接

這是我到目前爲止;

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

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

非常感謝, 山姆

+1

將用戶輸入存儲在'SQLite'或'SharedPreferences'中。檢索存儲的值並在'loadUrl'中使用 – tahsinRupam

+0

String extraInfo =「foo」; mWebView.loadUrl(「https://」+ extraInfo +「.mydomain.co.uk」); 那麼這有什麼問題? –

回答

0

商店使用sharedPrefeence如下變量,

SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE); 
SharedPreferences.Editor editor = sp.edit(); 
editor.putString("your_string_key", yourStringValue); 
editor.commit(); 

然後檢索值,

SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE); 
String myStringValue = sp.getInt("your_string_key", -1); 

使用在loadUrl作爲

變量
mWebView.loadUrl("https://"+myStringValue+".mydomain.co.uk"); 
+0

感謝你的支持,但是我有點卡住了,所以我通過打開問題進行了修改。你介意幫忙嗎? –

+0

您已聲明client_subdomain作爲方法displaydata的局部變量。在類中聲明變量。 –

+0

在onCreate()之前,私有WebView mWebView; private String client_subdomain; –

相關問題