布局檔案:
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();
}
};
}
轉載于:https://www.cnblogs.com/zhoujn/p/4156181.html