彈出帶View的對話框並操作其中的控件
public void openSet(View view){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
// Get the layout inflater
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
/**
*googleAPI寫法
* builder.setView(inflater.inflate(R.layout.server_url_set, null))
* 建議使用一下寫法
*/
final View serverURLView = inflater.inflate(R.layout.server_url_set, null);
// builder.setTitle("配置服務器URL");
builder.setView(serverURLView);
// Add action buttons
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//在findViewById方法前面必須寫空間所在的View,不寫則空指針異常
EditText serverURL = (EditText) serverURLView.findViewById(R.id.server_et_url);
System.out.println(serverURL.getText());
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
}).show();
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.