java Android Store变量及其在webview链接中的使用
我试图将用户输入的文本字段存储在共享首选项中,并在webview链接中使用该输入
以下是我目前掌握的情况
后勤活动。java
package com.example.app;
import 安卓.app.Activity;
import 安卓.content.SharedPreferences;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.widget.EditText;
import 安卓.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();
}
}
主要活动。java
package com.example.app;
import 安卓.app.Activity;
import 安卓.content.Context;
import 安卓.content.SharedPreferences;
import 安卓.os.Bundle;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.view.View;
import 安卓.webkit.WebSettings;
import 安卓.webkit.WebView;
import 安卓.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);
}
}
活动\u登录。xml
<LinearLayout
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:gravity="center_horizontal"
安卓:orientation="vertical"
安卓:paddingBottom="@dimen/activity_vertical_margin"
安卓:paddingLeft="@dimen/activity_horizontal_margin"
安卓:paddingRight="@dimen/activity_horizontal_margin"
安卓:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.app.LoginActivity">
<LinearLayout
安卓:id="@+id/email_login_form"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:orientation="vertical">
<EditText
安卓:id="@+id/YourSchool"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:hint="Your school"
安卓:maxLines="1"
安卓:singleLine="true" />
<Button
安卓:id="@+id/sign_in_button"
style="?安卓:textAppearanceSmall"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:layout_marginTop="16dp"
安卓:onClick="webView"
安卓:text="SIGN IN"
安卓:textStyle="bold" />
</LinearLayout>
</ScrollView>
</LinearLayout>
主要活动。xml
<RelativeLayout
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context=".MainActivity">
<WebView
安卓:id="@+id/activity_main_webview"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent" />
</RelativeLayout>
AndroidManifest。xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
package="com.example.app">
<uses-permission 安卓:name="安卓.permission.INTERNET" />
<!-- To auto-complete the email text field in the login form with the
user's emails -->
<uses-permission 安卓:name="安卓.permission.GET_ACCOUNTS" />
<uses-permission 安卓:name="安卓.permission.READ_PROFILE" />
<uses-permission 安卓:name="安卓.permission.READ_CONTACTS" />
<application
安卓:allowBackup="true"
安卓:icon="@drawable/ic_launcher"
安卓:label="@string/app_name"
安卓:theme="@安卓:style/Theme.NoTitleBar.Fullscreen">
<activity
安卓:name=".MainActivity"
安卓:label="@string/app_name"></activity>
<activity
安卓:name=".LoginActivity"
安卓:label="@string/title_activity_login">
<intent-filter>
<action 安卓:name="安卓.intent.action.MAIN" />
<category 安卓:name="安卓.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
</application>
</manifest>
我的问题是,客户端_子域在MainActivity中被标记为红色。当我试图构建这个项目时,我得到了一个错误:找不到符号变量client_子域
我想这可能是我错过的一件小事,但任何帮助都将不胜感激
非常感谢, 山姆
# 1 楼答案
使用SharedPrefence存储变量,如下所示
然后检索值,如下所示:
使用
loadUrl
中的变量作为