布局文件:
my_dlg.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="300dp" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="使用者姓名:" /> <EditText android:id="@+id/edtUserName" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码:" /> <EditText android:id="@+id/edtPassword" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:text="" /> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" > <Button android:id="@+id/btnOK" android:layout_width="160dp" android:layout_height="wrap_content" android:text="确定登陆" /> <Button android:id="@+id/btnCancel" android:layout_width="80dp" android:layout_height="wrap_content" android:text="取消" /> </LinearLayout> </LinearLayout>
main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:orientation="vertical" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" /> <LinearLayout android:orientation="vertical" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" > <Button android:id="@+id/btnLoginDlg" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="登入系統" /> <TextView android:id="@+id/txtResult" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" /> </LinearLayout> <LinearLayout android:orientation="vertical" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" /> </LinearLayout>
package tw.android; import android.app.Activity; import android.app.Dialog; import android.os.Bundle; import android.view.View; import android.widget.*; public class Main extends Activity { private Button mBtnLoginDlg; private TextView mTxtResult; private Dialog mLoginDlg; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setupViewComponent(); } private void setupViewComponent() { mBtnLoginDlg = (Button)findViewById(R.id.btnLoginDlg); mTxtResult = (TextView)findViewById(R.id.txtResult); mBtnLoginDlg.setOnClickListener(btnLoginDlgOnClkLis); } private Button.OnClickListener btnLoginDlgOnClkLis = new Button.OnClickListener() { public void onClick(View v) { mTxtResult.setText(""); mLoginDlg = new Dialog(Main.this);//建立的Dialog对象必须制定它的拥有者。 mLoginDlg.setTitle("登陆系统"); mLoginDlg.setCancelable(false);//设置Cancelable属性,让用户无法使用“回上一页”按钮离开对话框 mLoginDlg.setContentView(R.layout.login_dlg); Button loginBtnOK = (Button)mLoginDlg.findViewById(R.id.btnOK); Button loginBtnCancel = (Button)mLoginDlg.findViewById(R.id.btnCancel); loginBtnOK.setOnClickListener(loginDlgBtnOKOnClkLis); loginBtnCancel.setOnClickListener(loginDlgBtnCancelOnClkLis); mLoginDlg.show(); } }; private Button.OnClickListener loginDlgBtnOKOnClkLis = new Button.OnClickListener() { public void onClick(View v) { EditText edtUserName = (EditText)mLoginDlg.findViewById(R.id.edtUserName); EditText edtPassword = (EditText)mLoginDlg.findViewById(R.id.edtPassword); mTxtResult.setText("请你输入的使用者姓名:" + edtUserName.getText().toString() + "密码:" + edtPassword.getText().toString()); mLoginDlg.cancel(); } }; private Button.OnClickListener loginDlgBtnCancelOnClkLis = new Button.OnClickListener() { public void onClick(View v) { mTxtResult.setText("你按下"取消"按钮。"); mLoginDlg.cancel(); } }; }