Здесь рассказывается, как создать приложение, которое открывает сайты при нажатии на различные кнопки и меняет масштаб при нажатии на пункты меню. Предыдущую часть можно прочитать по адресу:
Если вы сделали DigestViewer по материалу первой части, то предлагаю ознакомиться с процессом улучшения приложения:
1. В файле MainActivity.java удалите все строки и вставьте:
package com.digestviewer;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import android.annotation.TargetApi;
import android.widget.LinearLayout;
import android.widget.Button;
import android.view.View;
public class MainActivity extends AppCompatActivity {
WebView webView;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
final Activity activity = this;
SharedPreferences sharedPref0 = activity.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor0 = sharedPref0.edit();
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_sc25:
webView.setInitialScale(25);
editor0.putInt(getString(R.string.saved_scale), 25);
editor0.apply();
return true;
case R.id.menu_sc50:
webView.setInitialScale(50);
editor0.putInt(getString(R.string.saved_scale), 50);
editor0.apply();
return true;
case R.id.menu_sc100:
webView.setInitialScale(100);
editor0.apply();
return true;
case R.id.menu_sc150:
webView.setInitialScale(150);
editor0.putInt(getString(R.string.saved_scale), 150);
editor0.apply();
return true;
case R.id.menu_sc200:
webView.setInitialScale(200);
editor0.putInt(getString(R.string.saved_scale), 200);
editor0.apply();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
webView = new WebView(this);
webView.getSettings().setJavaScriptEnabled(true);
final Activity activity = this;
webView.setWebViewClient(new WebViewClient() {
@SuppressWarnings("deprecation")
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
@TargetApi(android.os.Build.VERSION_CODES.M)
@Override
public void onReceivedError(WebView view, WebResourceRequest req, WebResourceError rerr) {
// Redirect to deprecated method, so you can use it in all SDK versions
onReceivedError(view, rerr.getErrorCode(), rerr.getDescription().toString(), req.getUrl().toString());
}
});
webView.loadUrl("http://www.tablepedia.com/");
//webView.setInitialScale(200);
SharedPreferences sharedPref0 = activity.getPreferences(Context.MODE_PRIVATE);
int highScore = sharedPref0.getInt(getString(R.string.saved_scale), 200);
webView.setInitialScale(highScore);
SharedPreferences sharedPref = getSharedPreferences("Selection", MODE_PRIVATE);
String actualJoin = sharedPref.getString(getString(R.string.saved_links), "1;2;3;4;5;6;7;8");
final String[] splitted = actualJoin.split(";");
LinearLayout buttonsView = new LinearLayout(this);
buttonsView.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); // Verbose!
lp2.setMargins(0, 0, 0, 0);
lp2.weight = 0.1f; // This is critical. Doesn't work without it.
LinearLayout.LayoutParams lpW = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); // Verbose!
lpW.setMargins(10, 10, 10, 10);
lpW.weight = 1.0f; // This is critical. Doesn't work without it.
LinearLayout row = new LinearLayout(this);
row.setOrientation(LinearLayout.HORIZONTAL);
row.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
Button btnBack = new Button(this);
btnBack.setText("<---");
btnBack.setTextSize(12);
btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
webView.goBack();
}
});
row.addView(btnBack, lp2);
Button btnT = new Button(this);
btnT.setText("tablepedia");
btnT.setTextSize(12);
btnT.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
webView.loadUrl("http://www.tablepedia.com/");
}
});
row.addView(btnT, lp2);
Button btnS = new Button(this);
btnS.setText("settings");
btnS.setTextSize(12);
btnS.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openActivity2();
}
});
row.addView(btnS, lp2);
Button btnForward = new Button(this);
btnForward.setText("--->");
btnForward.setTextSize(12);
btnForward.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
webView.goForward();
}
});
row.addView(btnForward, lp2);
LinearLayout row2 = new LinearLayout(this);
row2.setOrientation(LinearLayout.HORIZONTAL);
row2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
Button btnInfo1 = new Button(this);
btnInfo1.setText(splitted[0]);
btnInfo1.setTextSize(12);
btnInfo1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
webView.loadUrl(splitted[1]);
}
});
row2.addView(btnInfo1, lp2);
Button btnInfo2 = new Button(this);
btnInfo2.setText(splitted[2]);
btnInfo2.setTextSize(12);
btnInfo2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
webView.loadUrl(splitted[3]);
}
});
row2.addView(btnInfo2, lp2);
Button btnInfo3 = new Button(this);
btnInfo3.setText(splitted[4]);
btnInfo3.setTextSize(12);
btnInfo3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
webView.loadUrl(splitted[5]);
}
});
row2.addView(btnInfo3, lp2);
Button btnInfo4 = new Button(this);
btnInfo4.setText(splitted[6]);
btnInfo4.setTextSize(12);
btnInfo4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
webView.loadUrl(splitted[7]);
}
});
row2.addView(btnInfo4, lp2);
buttonsView.addView(row, lp2);
buttonsView.addView(webView, lpW);
buttonsView.addView(row2, lp2);
setContentView(buttonsView, lpW);
}
public void openActivity2() {
Intent intent = new Intent(this, inputActivity.class );
startActivity(intent);
}
}
2. Содержимое файла strings.xml должно быть таким:
<resources>
<string name="app_name">DigestViewer</string>
<string name="menu_sc25">25%</string>
<string name="menu_sc50">50%</string>
<string name="menu_sc100">100%</string>
<string name="menu_sc150">150%</string>
<string name="menu_sc200">200%</string>
<string name="saved_scale">save_high</string>
<string name="saved_links">saved_links</string>
</resources>
3. Создайте новый Activity под названием "inputActivity" через клик правой кнопке мыши на левой панели, при этом будет создан файл activity_input.xml (Рис. 2):
4. В файле inputActivity.java удалите все строки и вставьте:
package com.digestviewer;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
public class inputActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input);
LinearLayout buttonsView = new LinearLayout(this);
buttonsView.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); // Verbose!
lp2.setMargins(0, 0, 0, 0);
lp2.weight = 0.1f; // This is critical. Doesn't work without it.
LinearLayout.LayoutParams lpW = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); // Verbose!
lpW.setMargins(10, 10, 10, 10);
lpW.weight = 0.1f; // This is critical. Doesn't work without it.
LinearLayout row = new LinearLayout(this);
row.setOrientation(LinearLayout.HORIZONTAL);
row.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
LinearLayout row2 = new LinearLayout(this);
row2.setOrientation(LinearLayout.HORIZONTAL);
row2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
LinearLayout row3 = new LinearLayout(this);
row3.setOrientation(LinearLayout.HORIZONTAL);
row3.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
LinearLayout row4 = new LinearLayout(this);
row4.setOrientation(LinearLayout.HORIZONTAL);
row4.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
LinearLayout row5 = new LinearLayout(this);
row5.setOrientation(LinearLayout.HORIZONTAL);
row5.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
LinearLayout row6 = new LinearLayout(this);
row6.setOrientation(LinearLayout.HORIZONTAL);
row6.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
Button btnExit = new Button(this);
btnExit.setText("exit");
btnExit.setTextSize(12);
btnExit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openActivity1();
}
});
row.addView(btnExit, lp2);
final EditText txtA1 = new EditText(this);
row3.addView(txtA1, lp2);
final EditText txtA2 = new EditText(this);
row3.addView(txtA2, lp2);
final EditText txtB1 = new EditText(this);
row4.addView(txtB1, lp2);
final EditText txtB2 = new EditText(this);
row4.addView(txtB2, lp2);
final EditText txtC1 = new EditText(this);
row5.addView(txtC1, lp2);
final EditText txtC2 = new EditText(this);
row5.addView(txtC2, lp2);
final EditText txtD1 = new EditText(this);
row6.addView(txtD1, lp2);
final EditText txtD2 = new EditText(this);
row6.addView(txtD2, lp2);
SharedPreferences sharedPref;
sharedPref = getSharedPreferences("Selection", MODE_PRIVATE);
String actualJoin = sharedPref.getString(getString(R.string.saved_links), "noser");
String[] splitted = actualJoin.split(";");
txtA1.setText(splitted[0]);
txtA2.setText(splitted[1]);
txtB1.setText(splitted[2]);
txtB2.setText(splitted[3]);
txtC1.setText(splitted[4]);
txtC2.setText(splitted[5]);
txtD1.setText(splitted[6]);
txtD2.setText(splitted[7]);
final Button btnDefault = new Button(this);
btnDefault.setText("default");
btnDefault.setTextSize(12);
btnDefault.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txtA1.setText("Info1");
txtA2.setText("http://www.nanopedia.site");
txtB1.setText("Info2");
txtB2.setText("http://www.ipedia.site");
txtC1.setText("Info3");
txtC2.setText("http://www.telepedia.site");
txtD1.setText("Info4");
txtD2.setText("http://www.quatrecoin.com");
}
});
row2.addView(btnDefault, lp2);
final Button btnLoad = new Button(this);
btnLoad.setText("load");
btnLoad.setTextSize(12);
btnLoad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences sharedPref;
sharedPref = getSharedPreferences("Selection", MODE_PRIVATE);
String actualJoin = sharedPref.getString(getString(R.string.saved_links), "noser");
String[] splitted = actualJoin.split(";");
txtA1.setText(splitted[0]);
txtA2.setText(splitted[1]);
txtB1.setText(splitted[2]);
txtB2.setText(splitted[3]);
txtC1.setText(splitted[4]);
txtC2.setText(splitted[5]);
txtD1.setText(splitted[6]);
txtD2.setText(splitted[7]);
}
});
row2.addView(btnLoad, lp2);
Button btnSave = new Button(this);
btnSave.setText("save");
btnSave.setTextSize(12);
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences sharedPref = getSharedPreferences("Selection", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
String message1 = txtA1.getText()+";"+txtA2.getText()+";"+txtB1.getText()+";"+txtB2.getText()+";"+txtC1.getText()+";"+txtC2.getText()+";"+txtD1.getText()+";"+txtD2.getText();
editor.putString(getString(R.string.saved_links), message1);
editor.apply();
}
//private SharedPreferences getPreferences(int modePrivate) {
//}
});
row.addView(btnSave, lp2);
buttonsView.addView(row, lpW);
buttonsView.addView(row2, lpW);
buttonsView.addView(row3, lpW);
buttonsView.addView(row4, lpW);
buttonsView.addView(row5, lpW);
buttonsView.addView(row6, lpW);
addContentView(buttonsView, lpW);
}
public void openActivity1() {
Intent intent = new Intent(this, MainActivity.class );
startActivity(intent);
}
}
4. Теперь через кнопку Settings можно менять ссылки, а также названия нижних кнопок, только не забывайте нажимать на Save (Рис. 3):