如何检查选定的单选按钮?

问题描述

我尝试检查选定的单选按钮,

我发现了一个细节,但我无法粘贴。

现在我有一个(查看视图)错误

程序不知道选择了哪一个

我找不到解决方案。

我还能写什么?

请帮忙。

谢谢:蚂蚁Z picture from the problem

我的代码

public async Task<ActionResult> Edit(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        //ProductsModel productsModel = await db.ProductsModels.FindAsync(id);
        var products = await db.ProductsModels.Include(a =>a.categorieId).Include(a => a.users).FirstAsync(a => a.id == id);
        if (products == null)
        {
            return HttpNotFound();
        }


        var selectedOwnerId = products.users?.Id ?? string.Empty;
        var users = db.Users.Select(userItem => new SelectListItem
        {
            Text = userItem.Email,Value = userItem.Id,Selected = userItem.Id == selectedOwnerId
        }).ToSafeReadOnlyCollection();


        var selectedCategoryId = products.categorieId.id;
        var productCategories = db.ProductCategoriesModels
           .Select(a => new SelectListItem
           {
               Value = a.id.ToString(),Text = a.name,Selected = a.id == selectedCategoryId
           }).ToSafeReadOnlyCollection();



        var viewmodel = new productCreatEditviewmodel()
        {
            Products = products,productCategories = productCategories,users = users
        };
        //ViewBag.users = userList;
        //ViewBag.productcategorieId = new SelectList(db.ProductCategoriesModels,"id","Name",productsModel.productcategorieId);
        return View(viewmodel);
    }

解决方法

你应该做两件事来解决这个问题。

  1. 更正大小写敏感并更正View ViewView view

  2. 将函数移动到 onCreate 函数下方和 MainActivity 内部。

  3. 你实际上并不需要 1&2 来得到你想要的东西。

  4. 您可以复制粘贴此代码并开始工作。

代码:

package myapplication.Buttons;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.uploadedlobster.PwdHash.R;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class MainActivity extends AppCompatActivity {
    EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EditText textView = findViewById(R.id.text);
        Button button = findViewById(R.id.button);
        Button btn1 = (Button)findViewById(R.id.button);
        Button btn2 = (Button)findViewById(R.id.exit);
        editText = findViewById(R.id.text);
        RadioGroup radioGroup =  findViewById(R.id.radioGroup1);

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence,int i,int i1,int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence,int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (radioGroup.getCheckedRadioButtonId() == R.id.radioButton_green) {
                    //do something
                }
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // do something
            }
        } );
    }
}

快乐编码! :)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...