2017-04-19 55 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; –

相关问题