●實作練習:預期畫面 

AlertDialog

●實作練習:xml

(一)activity_main.xml

<Button
    android:id="@+id/seventhbtn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="seventhbtn"
    android:text="自訂選單" />

 

(二)fragment_dialogfragment.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.student.lab05.dialogfragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <EditText
            android:id="@+id/name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="15dp"
            android:layout_marginRight="5dp"
            android:layout_marginBottom="5dp"
            android:hint="Username"
            android:inputType="textEmailAddress" />

        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginBottom="5dp"
            android:fontFamily="sans-serif"
            android:hint="Password"
            android:inputType="textPassword" />

    </LinearLayout>

</FrameLayout>

 

●實作練習:java code

(一)MainActivity.java

public void seventhbtn(View view) {
    //建立自訂的dialog
    dialogfragment my=new dialogfragment();
    my.show(getSupportFragmentManager(),"dialogfragment");

}

public void changeContent(CharSequence msg){
    text.setText(msg);
}

public int times(){
    count=++count;
    return count;
}

 

(二)dialogfragment.java

//繼承對象由Fragment變更為android.support.v4.app.DialogFragment
//因為不是一般的Fragment,
//要刪除onCreateView,並且override DialogFragmentonCreateDialog
public class dialogfragment extends DialogFragment {
    private EditText name;
    int count;
    //private EditText password;

    public dialogfragment() {
        // Required empty public constructor
    }

    @NonNull
    @Override
    //Override to build your own custom Dialog container.
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        //Instantiates a layout XML file into its corresponding View objects.
        //透過inflater讀取fragment_dialogfragment.xml來產生view
        //取得inflater
        LayoutInflater inflater=getActivity().getLayoutInflater(); 

        //fragment_dialogfragment.xml取得自訂畫面,
         //作法inflate(resources,viewGroup)
        //因為這邊沒有viewgroup所以是null
        View v=inflater.inflate(R.layout.fragment_dialogfragment,null);
        name=v.findViewById(R.id.name);
        
        //Activity getActivity ()
        //Return the Activity this fragment is currently associated with.
        AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
        //AlertDialog.Builder setView (View view)
        //Sets a custom view to be the contents of the alert dialog.
        builder.setView(v)
            .setPositiveButton("SIGN IN", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    MainActivity main=(MainActivity)getActivity();
                    int count=main.times();
                    CharSequence Name=name.getText();
                    StringBuilder all=new StringBuilder();
                    all.append("次數")
                       .append(count)
                       .append(" 歡迎光臨 ")
                       .append(Name);
                    main.changeContent(all);
                }
            })
            .setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                   //先取得mainActivity,再透過寫在mainactivity中的 changeContent
                   //方法改變mainActivitytextview內容
                    MainActivity main=(MainActivity)getActivity();
                  main.changeContent("登入取消");
                }
            });

      //AlertDialog create ()
      //Creates an AlertDialog with the arguments supplied to this builder.
      //回傳建立的Dialog
      return builder.create();
    }
}

 

*程式參考(gitHub):AlertDialog實作

arrow
arrow
    創作者介紹
    創作者 muchone 的頭像
    muchone

    簡單。生活。享受

    muchone 發表在 痞客邦 留言(0) 人氣()