获取编程设置的 EditText 值
编辑4:我做了自己的数字选择器,所以我不再需要这方面的帮助。
但我认为问题是我没有使用dialog.findViewById()...
我试图获取一个以编程方式创建的 EditText
值。但它不起作用,当 onClick
函数运行代码时会中断。
button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
numberValue = (EditText) findViewById(R.id.number_picker_value);
Log.d("****2**", numberValue.getText().toString());
dialog.dismiss();
}
});
值和 ID 是从另一个类 NumberPicker.java
设置的。
private void initValueEditText( Context context )
{
value = new Integer( 0 );
valueText = new EditText( context );
valueText.setId(R.id.number_picker_value); //ID set here
valueText.setTextSize(25);
...
//value set sooner, look in the link for full code.
}
NumberPicker.java 的完整源代码可以在此处找到。
R.id.number_picker_value
在 XML 文件中定义为
。
编辑:
我所说的“代码中断”是指我得到一个强制关闭对话框
。
EDIT2:
Logcat 输出。(这是你想要的吗?)
EDIT3:
顺便说一句,我从不调用 NumberPicker.java。当我用这段代码加载 XML 文件时,它会自行启动:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content">
<!-- This seem to start NumberPicker.java, so I have no object to refer to. -->
<com.android.GissaSiffran.NumberPicker
android:id = "@+id/numberPickerDialog"
android:orientation = "horizontal"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:gravity = "center">
</com.android.GissaSiffran.NumberPicker>
<!-- Canel / ok button -->
<Button
android:id = "@+id/cnfrm"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:textSize = "13dp"
android:textStyle = "italic"
android:text = "@string/cnfrmBtn"></Button>
</LinearLayout>
也许我在这里做错了什么?
当我运行dialog.setContentView(R.layout.pick_a_number_dialog);时dialog.show(); XML 加载,数字选择器位于对话框中,我可以选择数字。 但我从来没有用 ie NumberPicker np = new NumberPicker(getApplicationContext(), null)); 启动
NumberPicker.java
也许我在这里做错了? (我是java新手)。
顺便说一句,这是我的第一篇文章。 :)
Edit 4: I did my own numberpicker, so I don't need help with this anymore.
But I think the problem is that I didn't use dialog.findViewById()...
I'm trying to get a EditText
value, that is created programmatic. But it don't work, when the onClick
function run the code breaks.
button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
numberValue = (EditText) findViewById(R.id.number_picker_value);
Log.d("****2**", numberValue.getText().toString());
dialog.dismiss();
}
});
The value and ID is set from another class, NumberPicker.java
.
private void initValueEditText( Context context )
{
value = new Integer( 0 );
valueText = new EditText( context );
valueText.setId(R.id.number_picker_value); //ID set here
valueText.setTextSize(25);
...
//value set sooner, look in the link for full code.
}
Full source code of NumberPicker.java can be found here here.
The R.id.number_picker_value
is defined in an XML file with<item type="id" name="number_picker_value" />
.
EDIT:
What I mean with "the code breaks" is that i get a Force close dialog
.
EDIT2:
Logcat output. (Is it this you want?)
EDIT3:
Btw, I never call on NumberPicker.java. It start itself when I load the XML file with this bit of code:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content">
<!-- This seem to start NumberPicker.java, so I have no object to refer to. -->
<com.android.GissaSiffran.NumberPicker
android:id = "@+id/numberPickerDialog"
android:orientation = "horizontal"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:gravity = "center">
</com.android.GissaSiffran.NumberPicker>
<!-- Canel / ok button -->
<Button
android:id = "@+id/cnfrm"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:textSize = "13dp"
android:textStyle = "italic"
android:text = "@string/cnfrmBtn"></Button>
</LinearLayout>
Maybe I do some wrong here?
When I run dialog.setContentView(R.layout.pick_a_number_dialog); dialog.show();
the XML loads and the number picker is in the dialog and i can choose number.
But I have never started the NumberPicker.java
with ie NumberPicker np = new NumberPicker(getApplicationContext(), null));
maybe I do wrong here? (I'm new to java).
Btw my first post here. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于未来的参考者,我使用的是相同的 NumberPicker.java 我猜。为了获取 EditText 的值,我在 OnCreateView 方法中创建了对 EditText 的引用,如下所示:
然后在 onClick 方法中:
For future referencers, I'm using the same NumberPicker.java I would guess. In order to get the value of the EditText, I created a reference to the EditText in the OnCreateView method like this:
Then in the onClick method: