针对(一)的ping程序,采用android的ConstraintLayout的布局方式来完成页面的组织,这个程序就一个界面,即activity_main.xml.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/lay_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.dzy.root.adrping.MainActivity">
<TextView
android:id="@+id/show_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/show_dsp"
android:ellipsize="middle"
android:textSize="20sp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="@id/lay_root"
app:layout_constraintLeft_toLeftOf="@+id/lay_root"
app:layout_constraintRight_toRightOf="@+id/lay_root"
app:layout_constraintTop_toTopOf="@id/lay_root"
android:layout_marginLeft="20sp"
android:layout_marginRight="20sp"/>
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/guideline"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
<AutoCompleteTextView
android:id="@+id/ipaddr_etxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/ip_dsp"
android:completionHint="@string/ip_lastfive"
android:textSize="20sp"
android:maxLines="1"
android:dropDownHorizontalOffset="20sp"
android:completionThreshold="1"
android:dropDownHeight="wrap_content"
android:dropDownWidth="match_parent"
app:layout_constraintBottom_toBottomOf="@id/show_txt"
app:layout_constraintLeft_toLeftOf="@+id/lay_root"
app:layout_constraintRight_toRightOf="@+id/lay_root"
app:layout_constraintTop_toTopOf="@id/lay_root"/>
<TextView
android:id="@+id/dst_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/dst_dsp"
android:ellipsize="middle"
android:textSize="20sp"
android:layout_marginTop="50sp"
app:layout_constraintBottom_toTopOf="@id/ipaddr_etxt"
app:layout_constraintLeft_toLeftOf="@+id/lay_root"
app:layout_constraintRight_toRightOf="@+id/lay_root"
app:layout_constraintTop_toTopOf="@id/lay_root"/>
<Button
android:id="@+id/start_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ping_dsp"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@id/lay_root"
app:layout_constraintLeft_toLeftOf="@id/lay_root"
app:layout_constraintRight_toRightOf="@id/guideline"
app:layout_constraintTop_toTopOf="@id/show_txt" />
<Button
android:id="@+id/cancel_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cancel_dsp"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@id/lay_root"
app:layout_constraintLeft_toLeftOf="@id/guideline"
app:layout_constraintRight_toRightOf="@id/lay_root"
app:layout_constraintTop_toTopOf="@id/show_txt" />
</android.support.constraint.ConstraintLayout>
show_txt是显示结果的文本,居中显示。
guideline是垂直中心线,用来对齐两个按钮的。
ipaddr_etxt是输入地址的edittext,可以自动补全,记录最近的5条ping,通过sharedPreference存储,具体代码需要java实现,后面会附带代码
start_btn和cancel_btn,分别是开始和停止的两个按钮。
ipaddr_etxt的记录java代码为MainActivity的方法。
private void saveHistory(String field,
AutoCompleteTextView autoCompleteTextView) {
String text = autoCompleteTextView.getText().toString();
SharedPreferences sp = getSharedPreferences("network_url", 0);
String longhistory = sp.getString(field, "www.csdn.net");
if (!longhistory.contains(text + ",")) {
StringBuilder sb = new StringBuilder(longhistory);
sb.insert(0, text + ",");
sp.edit().putString("history", sb.toString()).apply();
}
}
private void initAutoComplete(String field,
AutoCompleteTextView autoCompleteTextView) {
SharedPreferences sp = getSharedPreferences("network_url", 0);
String longhistory = sp.getString("history", "www.csdn.net");
String[] histories = longhistory.split(",");
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_dropdown_item_1line, histories);
// 只保留最近的50条的记录
if (histories.length > 50) {
String[] newHistories = new String[50];
System.arraycopy(histories, 0, newHistories, 0, 50);
adapter = new ArrayAdapter<>(this,
android.R.layout.simple_dropdown_item_1line, newHistories);
}
autoCompleteTextView.setAdapter(adapter);
autoCompleteTextView
.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
AutoCompleteTextView view = (AutoCompleteTextView) v;
if (hasFocus) {
view.showDropDown();
}
}
});
}
可以设置默认值,历史记录数等,调用的地方,参考 (一)的代码,分别在OnCreate和OnClick的方法中调用。
另外,图标的下载可以用easyicon,代码维护采用git,托管在github上,调试可以在adb设置为无线真机调试,adb的路径在Android/Sdk/platform-tools等等。
实际的布局效果为: