【问题标题】:Add buttons more buttons添加按钮 更多按钮
【发布时间】:2014-05-27 07:41:38
【问题描述】:

我试图放置多个按钮,但它不起作用:

    View scanbarcode = (Button) findViewById(R.id.scanbarcode);
    View facility = (Button) findViewById(R.id.facility);   

    // For Scan bar code
    scanbarcode.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(getBaseContext(), CameraPreviewActivity.class);
                        startActivity(intent);   

        }

    });

    // For facility menu 
    facility.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(getBaseContext(), FacilityActivity.class);
                        startActivity(intent);   
        }
    });

【问题讨论】:

  • 你有什么问题?
  • if {(您遇到了崩溃,请向我们展示 logcat 输出)} else {(请告诉我们什么不工作)}
  • 设施按钮不起作用

标签: java android sdk


【解决方案1】:

不是一个真正的答案,但我必须显示代码。如果将 View 更改为 Button 会怎样:

       Button scanbarcode = (Button) findViewById(R.id.scanbarcode);
       Button facility = (Button) findViewById(R.id.facility);   

       // For Scan bar code
      scanbarcode.setOnClickListener(new OnClickListener() {
           @Override
           public void onClick(View arg0) {

           Intent intent = new Intent(getBaseContext(),CameraPreviewActivity.class);
                 startActivity(intent);   

         }

           });

       // For facility menu 
         facility.setOnClickListener(new OnClickListener() {
        @Override
         public void onClick(View arg0) {
       Intent intent = new Intent(getBaseContext(), FacilityActivity.class);
                startActivity(intent);   
    }
  });

这应该可行。除此之外,您真的需要将按钮引用到正确的 layout.xml 吗?能给我们看看 layout.xml 文件吗?

【讨论】:

  • 我的意思是,简而言之:请向我们展示整个 Activity,或者让我们知道您在 setContentView(R.layout.your_layout); 中引用了哪个布局;并使用保存在项目中的名称发布整个 xml 布局
  • setContentView(R.layout.activity_viewer);
  • 你上面显示的按钮在里面的这个 xml 布局是如何被调用的?这是相同的布局吗?有时新手会尝试从错误的布局中引用按钮,这就是我问的原因。
猜你喜欢
  • 2014-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多