对话框如何传递参数到mainActivity?

zengting 2012-02-06 12:07:36
我在MainActivity里面创建了一个对话框代码如下:
LinearLayout loginLayout = (LinearLayout) getLayoutInflater().inflate(
R.layout.login, null);
new AlertDialog.Builder(this).setIcon(R.drawable.login)
.setTitle("用户登录").setView(loginLayout).setPositiveButton("登录",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,
int whichButton)
{
// 编写处理用户登录的代码
}
}).setNegativeButton("取消",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,
int whichButton)
{
// 取消用户登录,退出程序
}
}).show();


login的xml如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<TextView android:layout_width="wrap_content" android:text="用户名:"
android:textSize="20dp" android:layout_height="wrap_content" />
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="密 码:"
android:textSize="20dp" />
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content" android:password="true" />
</LinearLayout>

</LinearLayout>


我的问题是,如何在点击登录的时候,把用户名和密码传递到mainActivity?我发现R.java 里面没有login.xml 里面的button的id,那我怎样获得用户名和密码呢?
...全文
244 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
奇拉米苏 2012-02-06
  • 打赏
  • 举报
回复
可以查找帮助,用intent相关的控件.
vae819723280 2012-02-06
  • 打赏
  • 举报
回复
后面加上.Tostring()是获得的内容为字符串形式
wolfhoo 2012-02-06
  • 打赏
  • 举报
回复
获取用户名和密码的方法:调用控件的getText()方法即返回该空间的内容。
[Quote=引用 6 楼 enhancing 的回复:]

首先你的布局写得不仅不规范,而且id也没有加上,所以你找不到id。两外,要获取布局中的控件,需要像5楼所说那样,因为这个布局是对话框的布局,应该用loginLayout.findViewById(R.id.accountName)。你所用的方法只有在调用了setContentView(R.layout.login),让这个布局属于Activity,才能直接调用FindViewById找到相应的控……
[/Quote]
wolfhoo 2012-02-06
  • 打赏
  • 举报
回复
首先你的布局写得不仅不规范,而且id也没有加上,所以你找不到id。两外,要获取布局中的控件,需要像5楼所说那样,因为这个布局是对话框的布局,应该用loginLayout.findViewById(R.id.accountName)。你所用的方法只有在调用了setContentView(R.layout.login),让这个布局属于Activity,才能直接调用FindViewById找到相应的控件。
修正的布局文件如下,希望对你有用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<TextView
android:layout_width="wrap_content"
android:hint="用户名:"
android:textSize="20dp"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/accountName"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="密 码:"
android:textSize="20dp" />
<EditText
android:id="@+id/passwd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:password="true" />
</LinearLayout>
Utopia 2012-02-06
  • 打赏
  • 举报
回复
这里的View是dialog的View,所以要用loginLayout.findfindViewById();
zengting 2012-02-06
  • 打赏
  • 举报
回复
现在有id了,但我用
public void onClick(DialogInterface dialog,
int whichButton)
{
TextView edit=(TextView)findViewById(R.id.username_edit);
Log.d("log", edit.getText().toString());
}

里面点击后出错,是怎么回事呢?
TextView edit=(TextView)findViewById(R.id.username_edit);
获取不了R.id.username_edit数据吗?
淘淘大师 2012-02-06
  • 打赏
  • 举报
回复
你在Eclipse中Project==》Build Project 把项目编译下R.java中就会有Button的id了
zengting 2012-02-06
  • 打赏
  • 举报
回复
我的问题是对话框传参数,不是Activity传参数

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧