DialogFragment在Android APP上可以時常看到,常以提視窗、警示窗等樣式出現,今天來紀錄一下在Activity使用DialogFragment要如何傳值並使其呈現在DialogFragment上,以及如何使用自定義的Dialog樣式。
自定義Dialog樣式 .xml (參考:stackoverflow custom Dialog)
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
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="vertical" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:background="#ffffffff"> | |
<ImageView | |
android:layout_width="match_parent" | |
android:layout_height="60dp" | |
android:id="@+id/imgIcon" | |
android:gravity="center" | |
android:background="#DA5F6A" | |
android:src="@drawable/dialog_error" | |
android:scaleType="fitCenter" | |
android:contentDescription="@string/error_icon" /> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@+id/txt_info" | |
android:layout_below="@+id/imgIcon" | |
android:layout_marginTop="20dp" | |
android:layout_marginLeft="4dp" | |
android:layout_marginRight="4dp" | |
android:layout_marginBottom="20dp" | |
android:textSize="18sp" | |
android:textColor="#ff000000" | |
android:layout_centerHorizontal="true" | |
android:gravity="center_horizontal" /> | |
<Button | |
android:layout_width="wrap_content" | |
android:layout_height="30dp" | |
android:text="@string/ok" | |
android:id="@+id/btn_close" | |
android:gravity="center_vertical|center_horizontal" | |
android:layout_below="@+id/txt_info" | |
android:layout_marginBottom="20dp" | |
android:background="#DA5F6A" | |
android:layout_centerHorizontal="true" | |
android:textColor="#ffffffff" /> | |
</RelativeLayout> |
其中ImageView中使用的Icon來自於Google Icon,此網站有許多Icon,並有為各個平台做處理,Android的話就有提供hdpi、mdpi、xhdpi等解析度的圖片。
DialogFragmentHelper.java
MainActivity.java
DialogFragmentHelper.java
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
package com.example.solinari.dialogfragment; | |
import android.os.Bundle; | |
import android.support.annotation.Nullable; | |
import android.support.v4.app.DialogFragment; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.view.Window; | |
import android.widget.Button; | |
import android.widget.TextView; | |
/** | |
* Created by Solinari on 2016/12/4. | |
*/ | |
public class DialogFragmentHelper extends DialogFragment{ | |
public static DialogFragmentHelper newInstance(String msg){ | |
DialogFragmentHelper f = new DialogFragmentHelper(); | |
Bundle args = new Bundle(); | |
args.putString("msg",msg); | |
f.setArguments(args);//透過setArguments傳值 | |
return f; | |
} | |
@Nullable | |
@Override | |
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | |
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);//取消Dialog title列 | |
getDialog().setCanceledOnTouchOutside(false);//不能點擊Dialog以外區域 | |
View v = inflater.inflate(R.layout.dialog, container, false);//選擇自定義的layout | |
Button btn = (Button) v.findViewById(R.id.btn_close) ; | |
btn.setOnClickListener(new View.OnClickListener(){ | |
@Override | |
public void onClick(View view) { | |
dismiss();//Dialog關閉 | |
} | |
} ); | |
TextView txt = (TextView) v.findViewById(R.id.txt_info); | |
String msg = getArguments().getString("msg"); | |
txt.setText(msg); | |
return v; | |
} | |
} |
MainActivity.java
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
package com.example.solinari.dialogfragment; | |
import android.support.v4.app.DialogFragment; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
public class MainActivity extends AppCompatActivity { | |
EditText txtMsg; | |
Button btnShowDalog; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
txtMsg = (EditText) findViewById(R.id.txt_Msg); | |
btnShowDalog = (Button) findViewById(R.id.btn_showDialog); | |
btnShowDalog.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
DialogFragment dialog =DialogFragmentHelper.newInstance(txtMsg.getText().toString());//將EditText值傳送給DialogFragment | |
dialog.show(getSupportFragmentManager(),"dialog");//Dialog出現 | |
} | |
}); | |
} | |
} |