forked from getActivity/Toaster
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
114 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.hjq.toast; | ||
|
||
import android.content.Context; | ||
import android.os.Looper; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.TextView; | ||
import android.widget.Toast; | ||
|
||
/** | ||
* author : HJQ | ||
* github : https://github.com/getActivity/ToastUtils | ||
* time : 2018/09/17 | ||
* desc : 加强版Toast | ||
*/ | ||
final class XToast extends Toast { | ||
|
||
private TextView mTextView; | ||
|
||
XToast(Context context) { | ||
this(context, Looper.getMainLooper()); | ||
} | ||
|
||
XToast(Context context, Looper looper) { | ||
super(context, looper); | ||
} | ||
|
||
@Override | ||
public final void setView(View view) { | ||
super.setView(view); | ||
if (view instanceof TextView) { | ||
mTextView = (TextView) view; | ||
return; | ||
}else if (view instanceof ViewGroup) { | ||
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { | ||
if (((ViewGroup) view).getChildAt(i) instanceof TextView) { | ||
mTextView = (TextView) ((ViewGroup) view).getChildAt(i); | ||
return; | ||
} | ||
} | ||
} | ||
//如果设置的布局没有包含一个TextView则抛出异常,必须要有一个TextView | ||
throw new IllegalArgumentException("The layout must contain a TextView"); | ||
} | ||
|
||
@Override | ||
public final void setText(CharSequence s) { | ||
if (mTextView != null) { | ||
mTextView.setText(s); | ||
}else { | ||
super.setText(s); | ||
} | ||
} | ||
} |