Android實現自定義對話框效果:核心代碼:package com.example.diydialog;import android.os.Bundle;import android.app.Activity;import android.app.AlertDialog;import androi...
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(); } }
佈局文件相對簡單,就不貼出來了,可以根據代碼添加相應按鈕和文本框。