布局文件

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent" android:layout_height="match_parent">

android:layout_width="397dp"

android:layout_height="131dp">

android:id="@ id/namet"

tools:text="姓名">

Mainactivity中绑定按钮事件(Main布局就不给出了)

final Button diy=(Button)findViewById(R.id.diy);

diy.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

LayoutInflater flater= LayoutInflater.from(MainActivity.this);

final View dialogview=flater.inflate(R.layout.login,null);//布局文件转换为view

Dialog dialog=new AlertDialog.Builder(MainActivity.this).setTitle("登录")//设置标题

.setView(dialogview)

.setPositiveButton("确定",

new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

EditText ed=(EditText)dialogview.findViewById(R.id.edname);//获取弹窗中的组件

String msg=ed.getText().toString();

Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_SHORT).show();

}

}).setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

}

}).create();

dialog.show();

}

});

效果图:

9011206e233af8481b17a1ebdfd31361.png

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐