自定義Dialog介面回撥引數
阿新 • • 發佈:2019-01-06
基本上所有的app專案都會有自定義dialog,但有時如果把dialog單獨寫成一個類,我們就需要考慮到如何將dialog的引數回撥到activity或者fragment,當然有很多方法,這裡主要講的是通過介面回撥的方式來把dialog中的引數傳遞到activity或者fragment,主要講述的是一個介面回撥的思想,這種思想同樣可以應用到專案中的其他地方,其次這裡也闡述瞭如何將一個字串中的某部分文字變色,當然這是從專案中抽取出來的一部分,好了,一起來看下效果圖吧:
1、如何實現改變一個字串中的部分文字的顏色
1)、可以使用Github 專案上一個開源專案
2)、可以使用html程式碼,在這裡我使用的是該方法
然後 contentTv.setText(Html.fromHtml(s)) 傳入該字串即可String s= "累計本次您已請假<font color =\"#FFCB22\">" + one + "</font>次,<font color = \"#FFCB22\" >" + two + "</font>天;剩餘請假<font color = \"#FFCB22\">" + three + "</font>次,<font color = \"#FFCB22\">" + four + "</font>天。是否提交申請?")
2、在xml 程式碼里根據自己的需求定義佈局,注意的是dialog的圓角是通過自定義的shaper 來進行實現的
這裡relativelayout_backgound_shape_left.xml的程式碼如下
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/shape_gray_dialog_right_15" /> <item android:state_pressed="false" android:drawable="@drawable/shape_white_dialog_15"/> </selector>
relativelayout_backgound_shape_right.xml的程式碼如下:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/shape_gray_dialog_left_15" />
<item android:state_pressed="false" android:drawable="@drawable/shape_white_dialog_15"/>
</selector>
shape_gray_dialog_left_15.xml的程式碼如下:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:bottomRightRadius="@dimen/distance_15"/>
<solid android:color="@color/gray_e5e5e5" />
</shape>
shape_gray_dialog_right_15.xml的程式碼如下: <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:bottomLeftRadius="@dimen/distance_15"/>
<solid android:color="@color/gray_e5e5e5" />
</shape>
3、自定義一個DialogFactory,在裡面實現程式碼
public static AlertDialog showEditDialog(final Context context, final ResultEditListener listener) {
final AlertDialog alertDialog = new AlertDialog.Builder(context)
.create();
alertDialog.show();
final Window window = alertDialog.getWindow();
window.getDecorView().setBackgroundColor(context.getResources().getColor(R.color.transparent));
window.setContentView(R.layout.course_buy_dialog);
window.setLayout(
window.getContext().getResources().getDisplayMetrics().widthPixels,
WindowManager.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);
alertDialog.setCanceledOnTouchOutside(true);
final EditText etContent = (EditText) window.findViewById(R.id.et_content);
window.findViewById(R.id.tv_sure).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
listener.SureListener(etContent.getText().toString().trim());
}
}
});
window.findViewById(R.id.tv_cancle).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
listener.CancleListener();
}
}
});
return alertDialog;
}
4、定義一個介面
public interface ResultEditListener {
void SureListener(String s);
void CancleListener();
}
5、在activity或者fragment中進行呼叫,並實現對應的介面
public class MainActivity extends AppCompatActivity implementsView.OnClickListener ,DialogFactory.ResultEditListener{
private AlertDialog alertDialog;
private TextView tvEtShow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvEtShow = (TextView) findViewById(R.id.tv_ed_show);
tvEtShow.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.tv_ed_show:
alertDialog = DialogFactory.showEditDialog(MainActivity.this,MainActivity.this);
break;
}
}
/**
* 彈出帶有EditView的Dialog
*/
@Override
public void SureListener(String result) {
Toast.makeText(this, result, Toast.LENGTH_SHORT).show();
alertDialog.dismiss();
}
@Override
public void CancleListener() {
Toast.makeText(this, "您點選了取消", Toast.LENGTH_SHORT).show();
alertDialog.dismiss();
}
}
好了,自定義Dialog介面回撥就到這,當然,這上面只是舉一個例子,具體的可以參考原始碼
點選下載原始碼
覺得有用的小夥伴可以頂一下^_^