Android開發筆記(11)——DialogFragment & 點擊監聽,android開發fragment

來源:互聯網
上載者:User

Android開發筆記(11)——DialogFragment & 點擊監聽,android開發fragment

轉載請註明:http://www.cnblogs.com/igoslly/p/6931519.html

DialogFragment使用 & 點擊監聽

 

/* DialogFragment是用於Activity上展示一個介面的彈出框,如輸入框、警告框、確認框等
 * DialogFragment單獨設定layout XML檔案
 * 使用DialogFragment至少需要實現onCreateView或者onCreateDIalog方法;
 * onCreateView即使用定義的xml布局檔案展示Dialog
 * onCreateDialog即利用AlertDialog或者Dialog建立出Dialog。
 */

下面給出我自己寫的幾個執行個體:

1、修改姓名的輸入框

布局檔案:

1、輸入的EditBox控制項,用於輸入新使用者名稱

2、確認的ImageView,點擊監控並擷取輸入字元

3、退出的Button按鈕,不做任何修改

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:orientation="vertical">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <EditText            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="match_parent"            android:hint="Please enter new user name ..."            android:inputType="text"            android:id="@+id/opponent_name_edit"/>        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:background="@drawable/ic_edit"            android:id="@+id/opponent_name_confirm"/>    </LinearLayout>    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="BACK"        android:textSize="24sp"        android:layout_marginTop="10dp"        android:id="@+id/edit_dialog_back"/></LinearLayout>

 

2、選擇內容的點擊框

 

/* DialogFragment活動設定和一般Fragment相同
 * 我一般採用onCreateView方法
 */

public class EditDialogue extends DialogFragment {    private EditText mNewNameEditText;    private ImageView mNewNameConfirm;    private String player;    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){        View rootView =inflater.inflate(R.layout.opponent_name_edit,container);        mNewNameEditText = (EditText) rootView.findViewById(R.id.opponent_name_edit);        mNewNameConfirm = (ImageView) rootView.findViewById(R.id.opponent_name_confirm);        Button backButton = (Button) rootView.findViewById(R.id.edit_dialog_back);        backButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                player="";                dismiss();            }        });
    mNewNameConfirm.setOnClickListener(new View.OnClickListener() {
    @Override
     public void onClick(View v) {
    player = mNewNameEditText.getText().toString();
     setData();dismiss();
     }});

    return rootView; }}

 

/* 點擊檢測
 * 通常的按鈕點擊可以在XML檔案設定Onclick屬性,直接調用MainActivity方法
 */

anroid:OnClick="RecordName"

//Activity中設定函數

public void RecordName(View view){    EditBox editbox = findViewById(R.id.name_edit);    String newName = editbox.getText().toString();  // 此處getText()擷取為Editable類型,toString()轉換}

/* 點擊檢測
 * Fragment裡則採用OnclickLisener而進行監聽
 * OnclickLisener是View類的函數,故而不僅對Button,對繼承於View的控制項均可採用
 * 輸入框中同時也對ImageView進行了監聽,並發送資料關閉Dialog
 */

mNewNameConfirm.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View v) {            player = mNewNameEditText.getText().toString();            setData();dismiss();        }});

 

 本筆記內容均為個人學習整理,轉載請註明部落格園-igoslly

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.