Android测试:从Instrumentation充气并显示自定义Toast [英] Android Testing: Inflate and Display a custom Toast from Instrumentation

查看:130
本文介绍了Android测试:从Instrumentation充气并显示自定义Toast的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图显示自定义的Toast,但这是通过自动化测试而不是应用程序本身来显示的.

I am trying to display a custom Toast but doing so from my automated test rather than from the application itself.

布局无法正常工作.甚至可以扩充视图并从测试项目中显示出来吗?

The layout inflation does not work though. Is it even possible to inflate views and from the test project and display those?

起作用的是标准的Toast:

What does work is a standard Toast:

final Activity targetActivity = Solo.getCurrentActivity(); // Using Robotium to get current displayed Activity
Toast.makeText(targetActivity, "Hello from Instrumentation", Toast.LENGTH_SHORT).show();

以下是行不通的:

final Activity targetActivity = Solo.getCurrentActivity(); // Using Robotium to get current displayed Activity

LayoutInflater inflater = targetActivity.getLayoutInflater();                         
View layout = inflater.inflate(test.my.package.R.layout.my_custom_toast, null); // resource is located in test project
TextView text = (TextView) layout.findViewById(test.my.package.R.id.textToShow); // textview within the layout

text.setText("Hello from Instrumentation"); // here I get the NullPointerException

Toast toast = new Toast(targetActivity);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();


解决方案

获取新的LayoutInflater而不引用targetActivity

final Activity targetActivity = Solo.getCurrentActivity(); // Using Robotium to get current displayed Activity

// *** !!! ***
LayoutInflater inflater = 
(LayoutInflater) getInstrumentation().getContext().getSystemService  (Context.LAYOUT_INFLATER_SERVICE); 
 // getContext() , NOT getTargetContext()

View layout = inflater.inflate(test.my.package.R.layout.my_custom_toast, null); // resource is located in test project
TextView text = (TextView) layout.findViewById(test.my.package.R.id.textToShow); // textview within the layout

text.setText("Hello from Instrumentation"); // here I get the NullPointerException

Toast toast = new Toast(targetActivity);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();

推荐答案

使用以下代码:

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.example.R;

public class DoToast extends Toast {

    /** The Constant TOAST_DURATION. */
    private static final int TOAST_DURATION=4000;

    /** The layout. */
    View layout; 

    /**
     * Instantiates a new do toast.
     *
     * @param context the context
     * @param text the text
     */
    public DoToast(Context context, CharSequence text) {
        super(context);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
        layout = inflater.inflate(R.layout.toast, null);
        TextView textView = (TextView) layout.findViewById(R.id.text);
        textView.setText(text);
        DoToast.this.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
        DoToast.this.setDuration(TOAST_DURATION);
        DoToast.this.setView(layout);
        DoToast.this.show();
    }
}

布局xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/toast_layout_root"
     android:orientation="horizontal"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:padding="8dp"
     android:layout_marginLeft="10dp"
     android:layout_marginRight="10dp"
     android:background="@drawable/toast_bg">

     <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:textColor="#000000" />
</LinearLayout>

和样式文件:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="rectangle" >
   <gradient
        android:angle="90"
        android:endColor="#ffffff"
        android:startColor="#F2F2F2"
        android:type="linear" />
    <stroke
        android:width="3dp"
        android:color="#000000" />
</shape>

如果您需要烤面包,则只需使用以下行:

If you need to show toast then just use the below line:

new DoToast(this,"Testing");  

如果您有任何疑问,请告诉我.

Let me know if you have any queries..

这篇关于Android测试:从Instrumentation充气并显示自定义Toast的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆