需求:
需要在应用中展示一个简单的使用说明,主要是业务上由于使用本应用前要先下载一个其他应用。
由于刚入门安卓开发等原因,使用最小成本简单实现,效果如下。
Activity.java中主要代码如下, 在onCreate()中调用initEvent();
注意:模拟器第一次运行时复制没有成功,系统提示无法获得剪贴板,但重启应用后复制成功且无错误提示。功能还未在用户手机上进行验证,问题是否会复现未知。
private void initEvent() {
findViewById(R.id.hint_btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showDialog();
}
});
}
private void showDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("使用说明");
builder.setMessage(
"请提前下载应用并进行连接,可点击下方复制按钮复制下载连接到网页打开进行下载。");
builder.setPositiveButton("复制下载连接",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
copyDownloadLink();
showToast("复制成功");
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
private void copyDownloadLink() {
// Gets a handle to the clipboard service.
if (null == mClipboard) {
mClipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
}
// Creates a new text clip to put on the clipboard
ClipData clip = ClipData.newPlainText("download link","http://www.baidu.com");
// Set the clipboard's primary clip.
mClipboard.setPrimaryClip(clip);
}
/**
* 这里的showToast方法是由于业务中用户使用的手机在输入密码时,
* 提示在下方会被键盘遮挡,故将提示移到了上方。
* 但本方法在模拟器中不生效,toast仍会在下方进行展示,
* 但模拟器中toast会在键盘上方进行展示故未进行问题排查。
*/
private void showToast(String text) {
Toast toast = Toast.makeText(LoginActivity.this, text, Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP, 0, 100);
toast.show();
}
activity对应的xml中代码如下,由于右上角对齐在模拟器中有问题,且我对这一块不太熟悉照搬如下。部分是直接在Design中直接拖动的。
<Button
android:id="@+id/hint_btn"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_gravity="top|right|end"
android:ems="10"
android:text="使用说明"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.885"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/guideline7" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.05" />