Android消息对话框实例
按着书敲得(大神绕行),敲多了就会了,哈哈哈!一起加油!
main.xml
<?xml version="1.0" encoding="utf-8"?><Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="普通对话框"
android:textSize="20sp"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="输入对话框"
android:textSize="20sp"/>
login.xml
<?xml version="1.0" encoding="utf-8"?><TextView
android:id="@+id/user"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="用户名"
android:textSize="18sp"/>
<EditText
android:id="@+id/userEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"/>
<TextView
android:id="@+id/password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="密码"
android:textSize="18sp"/>
<EditText
android:id="@+id/paswdEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"/>
main.java
package com.example.myapplication3;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
ProgressDialog mydialog;
Button btn1,btn2;
LinearLayout login;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=findViewById(R.id.button1);
btn2=findViewById(R.id.button2);
btn1.setOnClickListener(new mClick());
btn2.setOnClickListener(new mClick());
}
class mClick implements OnClickListener{
Builder dialog=new AlertDialog.Builder(MainActivity.this);
@Override
public void onClick(View arg0){
if(arg0btn1){
// 设置对话框的标题,图标,提示内容,按钮操作
dialog.setTitle(“警告”);
dialog.setIcon(R.drawable.icon1);
dialog.setMessage(“本项操作可能导致信息泄露!”);
dialog.setPositiveButton(“确定”,new okClick());
// 创建显和示对话框
dialog.create();
dialog.show();
}
else if (arg0btn2){
// 从另外的布局关联组件
login=(LinearLayout)getLayoutInflater()
.inflate(R.layout.login,null);
dialog.setTitle(“用户登录”).setMessage(“请输入用户名和密码”)
.setView(login);
dialog.setPositiveButton(“确定”,new loginClick());
dialog.setNegativeButton(“退出”,new exitClick());
dialog.setIcon(R.drawable.icon2);
dialog.create();
dialog.show();
}
}
}
class okClick implements DialogInterface.OnClickListener{
@Override
public void onClick(DialogInterface dialog, int which){
// 关闭对话框
dialog.cancel();
}
}
class loginClick implements DialogInterface.OnClickListener {
EditText txt;
@Override
public void onClick (DialogInterface dialog,int which){
txt= login.findViewById(R.id.paswdEdit);
if ((txt.getText().toString()).equals(“admin”))
Toast.makeText(getApplicationContext(),
“登录成功”, Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplicationContext(),
“密码错误”,Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}
class exitClick implements DialogInterface.OnClickListener{
@Override
public void onClick(DialogInterface dialog,int which){
MainActivity.this.finish();
}
}
}