如何集中定制的创作干杯 [英] How to centralize a custom toast creation
本文介绍了如何集中定制的创作干杯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用程序中使用自定义土司几乎所有的活动。要创建自定义的敬酒,我有以下方法:
in my application i use a custom toast in almost all the activity. To create the custom toast i have the following method :
private void getCustomToast(String message)
{
LayoutInflater li = getLayoutInflater();
View toastlayout = li.inflate(R.layout.toast_error, (ViewGroup)findViewById(R.id.toast_layout));
TextView text = (TextView) toastlayout.findViewById(R.id.toast_text);
text.setText(message);
Toast toast = new Toast(this);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(toastlayout);
toast.show();
}
它工作正常,但每一项活动,我需要重复这种方法,而不是真的respectfull的DRY原则......
It works fine but for each activity I need to duplicate this method, not really respectfull of the DRY principle ...
我怎样才能让一个静态类(例如),其中我有要去火的方法对当前活动自定义敬酒?
How can i make a static class (for example) in which i have a method which gonna fire the custom toast on the current activity ?
感谢
推荐答案
您应该包含烤面包的方法自定义抽象的活动,然后扩大,对应用程序的活动:
You should make a custom abstract Activity that contains the toast method, and then extend that for your application's activities:
public abstract class ToastActivity extends Activity {
protected void getCustomToast(String message)
{
LayoutInflater li = getLayoutInflater();
View toastlayout = li.inflate(
R.layout.toast_error,
(ViewGroup) findViewById(R.id.toast_layout));
TextView text = (TextView) toastlayout.findViewById(R.id.toast_text);
text.setText(message);
Toast toast = new Toast(this);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(toastlayout);
toast.show();
}
}
这篇关于如何集中定制的创作干杯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文