获取编程设置的 EditText 值

发布于 2024-12-09 18:22:49 字数 2738 浏览 0 评论 0原文

编辑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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小兔几 2024-12-16 18:22:49

对于未来的参考者,我使用的是相同的 NumberPicker.java 我猜。为了获取 EditText 的值,我在 OnCreateView 方法中创建了对 EditText 的引用,如下所示:

private EditText txtQty;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.dialog_numberpicker,container,false);

    txtQty = (EditText) view.findViewById(idText);

    return view;
}  

然后在 onClick 方法中:

String newOnHand = txtQty.getText().toString();

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:

private EditText txtQty;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.dialog_numberpicker,container,false);

    txtQty = (EditText) view.findViewById(idText);

    return view;
}  

Then in the onClick method:

String newOnHand = txtQty.getText().toString();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文