【问题标题】:How to get a String Value from a void view in android?如何从 android 的 void 视图中获取字符串值?
【发布时间】:2012-10-12 19:38:37
【问题描述】:

我正在尝试从下面的 Button onClick 方法中获取字符串 firstChoicesecondChoice 的值,并在同一个类中使用它。问题是我知道您不能从 void 方法返回 String 值。请看我下面的内容:

 public class Question extends Activity implements OnClickListener{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question);

        Choice1 = (Button) findViewById(R.id.Choice4);
        Choice1.setOnClickListener(Question.this);

        Choice2 = (Button) findViewById(R.id.Choice5);
        Choice2.setOnClickListener(Question.this);

        firstChoice;
        secondChoice;

    public void onClick(View view) {
            switch(view.getId()) {
              case R.id.Choice4:
                  showSelectPicksDialog();
                  Button b = (Button)view;
                String firstChoice = b.getText().toString();

                break;

              case R.id.Choice5:
                  showSelectPicksDialog2();
                  Button v = (Button)view;
                String secondChoice = v.getText().toString();// 
                break;

              default:
                break;
            }
          }

有没有办法获得这些值?就像现在一样,当我尝试调用 on Create 方法中的值时,我收到错误消息,指出 firstChoice 和 secondChoice “无法解析为类型”。

【问题讨论】:

    标签: android string button void


    【解决方案1】:

    有很多方法,例如可以使用类变量:

    public class Question extends Activity implements OnClickListener{
        String firstChoice;    
        String secondChoice;    
        Button choice1;
        Button choice2;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_question);
    
            choice1 = (Button) findViewById(R.id.Choice4);
            choice1.setOnClickListener(Question.this);
    
            choice2 = (Button) findViewById(R.id.Choice5);
            choice2.setOnClickListener(Question.this);
    
            public void onClick(View view) {
                switch(view.getId()) {
                case R.id.Choice4:
                    showSelectPicksDialog();
                    Button b = (Button)view;
                    firstChoice = b.getText().toString();
                    break;
    
                case R.id.Choice5:
                    showSelectPicksDialog2();
                    Button v = (Button)view;
                    secondChoice = v.getText().toString();
                    break;
    
                default:
                    break;
                }
            }
        }
    }
    

    【讨论】:

    • 感谢您的回复。我现在正在尝试这个。它应该使 firstChoice 和 secondChoice 变量的值正确吗?就算public void onClick(View view) 也是一个void?
    • 是的,firstChoicesecondChoice 不会被返回,所以 void 返回类型无关紧要。
    猜你喜欢
    • 1970-01-01
    • 2016-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-20
    • 2011-10-05
    • 2017-07-25
    • 1970-01-01
    相关资源
    最近更新 更多