獲取 AlertDialog自定義的布局 的控件


AlertDialog自定義的布局

 效果圖:

 

 

 創建dialog方法的代碼如下:

1 LayoutInflater inflater = getLayoutInflater();
2      View layout = inflater.inflate(R.layout.dialog, 3    (ViewGroup) findViewById(R.id.dialog)); 4    new AlertDialog.Builder(this).setTitle("自定義布局").setView(layout) 5    .setPositiveButton("確定", null) 6    .setNegativeButton("取消", null).show();

這就實現了自定布局的AlertDialog

 

dialog布局文件代碼如下:

<?xml version="1.0" encoding="utf-8"?>
 
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_height="wrap_content" android:layout_width="wrap_content"
   android:background="#ffffffff" android:orientation="horizontal"
   android:id="@+id/dialog">
   <TextView android:layout_height="wrap_content"
     android:layout_width="wrap_content"
    android:id="@+id/tvname" android:text="姓名:" />
   <EditText android:layout_height="wrap_content"
    android:layout_width="wrap_content" android:id="@+id/etname" android:minWidth="100dip"/>
  </LinearLayout>

 

 

  

現實使用時還需要獲取自定義布局內的控件數據,這個要怎么做呢?

下面就來說說吧:

上面可以看到我們已經吧布局賦給了layout,所以獲取控件肯定是

要從layout獲取了。

獲取方法:

例如我們獲取的是一個EdiText控件就通過layout的findViewById的方法就可以獲取到,因為

是通過layout獲取到的所以一定是個View類型,所以一樣需要強制轉換成EdiText類型。

EditText jiumima = (EditText) layout.findViewById(R.id.setting_jiupass);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM