歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android自定義對話框

Android自定義對話框

日期:2017/3/1 10:17:01   编辑:Linux編程

本文的Android自定義對話框效果圖如下:

代碼實現如下:
CustomDialogActivity.java文件
package wzhnsc.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

public class CustomDialogActivity extends Activity {


private Button custom = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

custom = (Button)this.findViewById(R.id.custom);
custom.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
showCustomDia();
}
});
}


private void showCustomDia()
{
AlertDialog.Builder customDia=new AlertDialog.Builder(CustomDialogActivity.this);
final View viewDia=LayoutInflater.from(CustomDialogActivity.this).inflate(R.layout.custom_dialog, null);
customDia.setTitle("新建文件夾");
customDia.setView(viewDia);
customDia.setPositiveButton("確定", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
EditText diaInput=(EditText) viewDia.findViewById(R.id.file_name);


}
});
customDia.create().show();
}

}

main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
android:id="@+id/custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自定義對話框" />

</LinearLayout>

custom_dialog.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="請輸入名字:" >
</TextView>
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/file_name"
android:lines="1">
</EditText>
</LinearLayout>

Copyright © Linux教程網 All Rights Reserved