Android 自定义对话框
Android实现自定义对话框效果:
核心代码:
package com.example.diydialog; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.view.Menu; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public void btn_dialog(View v){ AlertDialog.Builder builder=new AlertDialog.Builder(this); builder.setTitle("用户登录"); builder.setIcon(R.drawable.ic_launcher); final View view=getLayoutInflater().inflate(R.layout.login, null); builder.setView(view); builder.setPositiveButton("登录",new OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO 自动生成的方法存根 EditText ed1= (EditText) view.findViewById(R.id.editText1); EditText ed2=(EditText) view.findViewById(R.id.editText2); String ed11=ed1.getText().toString(); String ed22=ed2.getText().toString(); Toast.makeText(MainActivity.this,ed11+"---"+ed22, Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO 自动生成的方法存根 Toast.makeText(MainActivity.this,"已经取消", Toast.LENGTH_SHORT).show(); } }); builder.show(); } }
布局文件相对简单,就不贴出来了,可以根据代码添加相应按钮和文本框。