我正在实施 Android Room 数据库,在其中一个教程中我发现了androidx.legacy:legacy-support-v4:1.0.0依赖项的用法。任何人都可以告诉我如何使用这种依赖项。
是否可以在按下某个按钮后跟踪哪个活动被打开?
我有一个测试,当点击/按下按钮时,它会向服务器发送一个请求.直到发送请求的时间,它会打开一个活动.为了验证测试的成功执行,我需要检查什么是打开的Activity.
我的测试示例:
检查Espresso中打开的Intent ---
private void startTest() {
recreateAuthData(InstrumentationRegistry.getTargetContext(), "d78269d9-9e00-4b8d-9242-815204b0a2f6", "3f32da21-914d-4adc-b6a1-891b842a2972");
InstrumentationRegistry.getTargetContext().getSharedPreferences(ActivitySplashScreen.class.getSimpleName(),
Context.MODE_PRIVATE).edit().putInt(ActivitySplashScreen.PROPERTY_APP_VERSION, ActivitySplashScreen.getAppVersion(InstrumentationRegistry.getTargetContext())).commit();
InstrumentationRegistry.getTargetContext().getSharedPreferences(ActivitySplashScreen.class.getSimpleName(),
Context.MODE_PRIVATE).edit().putString(ActivitySplashScreen.PROPERTY_REG_ID, "testKey").commit();
mActivityRule.launchActivity(setIntent());
// inputPinCode("2794");
}
@Test
public void testIdent() {
startTest();
onView(withText("???")).perform(click());
putDelay(500);
onView(withId(R.id.get_pro)).perform(click());
onView(withText("??????????? ?? ?????????? ??????")).perform(click());
putDelay(500);
closeSoftKeyboard();
onView(withId(R.id.btn_goto_passport)).perform(click());
onView(withHint("????? ? ????? ????????")).perform(replaceText("9894657891"));
onView(withHint("???? ?????? ????????")).perform(replaceText("17032014"));
onView(withHint("???? ????????")).perform(replaceText("31091994"));
onView(withHint("?????")).perform(replaceText("54665285919"));
putDelay(500);
Log.d("TestWidget", hasComponent(hasShortClassName("ActivityMain")).toString());
onView(withId(R.id.btn_next)).perform(click());
// some code which check which activity is display now
putDelay(500);
}
Run Code Online (Sandbox Code Playgroud) 我有一个TextView会在点击按钮时显示不同的文字.但问题是,按钮位置因TextView尺寸而改变.我不希望在不使用边距的情况下更改按钮位置.请帮忙.
我的设计XML代码.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.arnab.androiddevelopmentfundamentals.MainActivity">
<TextView
android:id="@+id/MyTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp"
android:text="@string/ui_and_events"
android:textSize="30sp" />
<TextView
android:id="@+id/MyText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/MyTitle"
android:layout_centerHorizontal="true"
android:layout_marginTop="39dp"
android:text="@string/my_text"
android:textColor="@color/colorPrimary"
android:textSize="30sp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/MyText"
android:layout_below="@+id/MyText"
android:layout_marginTop="33dp"
android:text="@string/change_1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="48dp"
android:text="@string/change_2" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
我点击按钮的java代码.
package com.example.arnab.androiddevelopmentfundamentals;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
TextView MyText;
Button …Run Code Online (Sandbox Code Playgroud)