Здесь рассказывается, как усовершенствовать приложение, чтобы оно загружало пользовательские ссылки и позволяло открывать их через "нижние кнопки". Предыдущие части можно прочитать по адресам:
Если вы сделали 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 sharedPref = getSharedPreferences("Selection", MODE_PRIVATE);
String actualJoin = sharedPref.getString(getString(R.string.saved_links)+";;;;;;;;;;", "Info1;http://nanopedia.site/;Info2;http://ipedia.site/;Info3;http://telepedia.site/;Info4;http://www.quatrecoin.com/");
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. В файле 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));
LinearLayout row7 = new LinearLayout(this);
row7.setOrientation(LinearLayout.HORIZONTAL);
row7.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);
final EditText txtMulti = new EditText(this);
txtMulti.setMaxLines(10);
row7.addView(txtMulti, lp2);
SharedPreferences sharedPref;
sharedPref = getSharedPreferences("Selection", MODE_PRIVATE);
String actualJoin = sharedPref.getString(getString(R.string.saved_links)+";;;;;;;;;;", "Info1;http://nanopedia.site/;Info2;http://ipedia.site/;Info3;http://telepedia.site/;Info4;http://www.quatrecoin.com/");
String[] splitted = actualJoin.split(";");
char someChar = ';';
int count = 0;
//if (actualJoin.length()>5) {
// for (int i = 0; i < actualJoin.length(); i++) {
// if (actualJoin.charAt(i) == someChar) {
//if (count == 0)
txtA1.setText(splitted[0]);
//if (count == 1)
txtA2.setText(splitted[1]);
//if (count == 2)
txtB1.setText(splitted[2]);
//if (count == 3)
txtB2.setText(splitted[3]);
//if (count == 4)
txtC1.setText(splitted[4]);
//if (count == 5)
txtC2.setText(splitted[5]);
//if (count == 6)
txtD1.setText(splitted[6]);
//if (count == 7)
txtD2.setText(splitted[7]);
// count++;
// }
// }
//}
final Button btnDefault = new Button(this);
btnDefault.setText("User Links");
btnDefault.setTextSize(12);
btnDefault.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String message = txtMulti.getText().toString().replaceAll("\\n", ";")+";";
message = message.replaceAll(";;;", ";");
message = message.replaceAll(";;", ";");
String[] splitted2 = message.split(";");
char someChar = ';';
int count = 0;
if (message.length()>5) {
for (int i = 0; i < message.length(); i++) {
if (message.charAt(i) == someChar) {
if (count == 0) txtA1.setText(splitted2[0]);
if (count == 1) txtA2.setText(splitted2[1]);
if (count == 2) txtB1.setText(splitted2[2]);
if (count == 3) txtB2.setText(splitted2[3]);
if (count == 4) txtC1.setText(splitted2[4]);
if (count == 5) txtC2.setText(splitted2[5]);
if (count == 6) txtD1.setText(splitted2[6]);
if (count == 7) txtD2.setText(splitted2[7]);
count++;
}
}
}
}
});
row2.addView(btnDefault, lp2);
final Button btnLoad = new Button(this);
btnLoad.setText("Cancel");
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)+";;;;;;;;;;", "Info1;http://nanopedia.site/;Info2;http://ipedia.site/;Info3;http://telepedia.site/;Info4;http://www.quatrecoin.com/");
String[] splitted = actualJoin.split(";");
char someChar = ';';
int count = 0;
//if (actualJoin.length()>5) {
// for (int i = 0; i < actualJoin.length(); i++) {
// if (actualJoin.charAt(i) == someChar) {
//if (count == 0)
txtA1.setText(splitted[0]);
//if (count == 1)
txtA2.setText(splitted[1]);
//if (count == 2)
txtB1.setText(splitted[2]);
//if (count == 3)
txtB2.setText(splitted[3]);
//if (count == 4)
txtC1.setText(splitted[4]);
//if (count == 5)
txtC2.setText(splitted[5]);
//if (count == 6)
txtD1.setText(splitted[6]);
//if (count == 7)
txtD2.setText(splitted[7]);
//count++;
// }
// }
//}
}
});
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) {
//mainF();
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();
}
});
row.addView(btnSave, lp2);
buttonsView.addView(row, lpW);
buttonsView.addView(row3, lpW);
buttonsView.addView(row4, lpW);
buttonsView.addView(row5, lpW);
buttonsView.addView(row6, lpW);
buttonsView.addView(row2, lpW);
buttonsView.addView(row7, lpW);
addContentView(buttonsView, lpW);
}
public void openActivity1() {
Intent intent = new Intent(this, MainActivity.class );
startActivity(intent);
}
}
3. Теперь через кнопку Settings можно менять ссылки и названия нижних кнопок, при этом можно скопировать в буфер обмена список ссылок (желательно, чтобы это были 8 строк, нечётные - наименования кнопок, чётные - ссылки), например:
Info1
http://nanopedia.site/
Info2
http://ipedia.site/
Info3
http://telepedia.site/
Info4
http://www.quatrecoin.com/