您的位置:首页 > 移动开发 > Android开发

android 自定义对话框 保存设置数据

2015-06-24 20:43 501 查看
android 自定义对话框

sharedPreferences = getSharedPreferences(
"scanbookSet", Context.MODE_PRIVATE);

Editor editor = sharedPreferences.edit();// 获取编辑器
// 创建对话框

AlertDialog.Builder dlg = new AlertDialog.Builder(MySocket.this);

dlg.setTitle("修改ip和端口");
View DialogView = LayoutInflater.from(MySocket.this).inflate(
R.layout.dialog, null);
final TextView tv_ipL = (TextView) DialogView
.findViewById(R.id.dlg_ipLabel);
final TextView tv_portL = (TextView) DialogView
.findViewById(R.id.dlg_portLabel);
final EditText et_ip = (EditText) DialogView
.findViewById(R.id.dlg_IP);
final EditText et_port = (EditText) DialogView
.findViewById(R.id.dlg_port);

dlg.setView(DialogView);// 设置自定义对话框的样式

dlg.setPositiveButton("保存", // 设置"确定"按钮

new DialogInterface.OnClickListener() // 设置事件监听
{

public void onClick(DialogInterface dialog,
int whichButton) {

String ip = et_ip.getText().toString();
String port = et_port.getText().toString();

editor.putString("ip", ip);

editor.putString("port", port);

editor.commit();// 提交修改
Toast.makeText(MySocket.this,
"您输入的内容是:ip:" + ip + "端口:" + port,
Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("取消", null).show();
}

});


dialog.xml

<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Socket" >

<EditText
android:id="@+id/dlg_IP"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="192.168.1.124" >
<requestFocus />
</EditText>

<TextView
android:id="@+id/dlg_ipLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/dlg_IP"
android:layout_alignBottom="@+id/dlg_IP"
android:layout_alignParentLeft="true"
android:layout_marginLeft="17dp"
android:text="@string/ipLabel" />

<TextView
android:id="@+id/dlg_portLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/dlg_ipLabel"
android:layout_below="@+id/dlg_IP"
android:layout_marginTop="21dp"
android:text="@string/portLabel" />

<EditText
android:id="@+id/dlg_port"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/dlg_portLabel"
android:layout_alignBottom="@+id/dlg_portLabel"
android:layout_alignLeft="@+id/dlg_IP"
android:ems="10"
android:text="8002" >
<requestFocus />

</EditText>

<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/dlg_port"
android:layout_below="@+id/dlg_port"
android:layout_marginTop="36dp"
android:src="@drawable/line" />

</RelativeLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息