android - 带有按钮背景和 Activity 布局的 If 语句

标签 android layout button if-statement

如何调用按钮背景,以及在 if 语句中匹配的 Activity 布局。不知道该怎么做。

  Context mContext = getApplicationContext();
  final Dialog dialog = new Dialog(mContext);
  dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

  final Button quicktab = (Button) findViewById(R.id.btnA4);
  quicktab.setOnClickListener(new OnClickListener()
  {
      @Override
      public void onClick(View v)
      {
          Button a4 = (Button)findViewById(R.id.btnA4);
          if ( a4.getBackground() == (R.drawable.quick_btn_background));      ****ERROR****
          { 
              if (this.setContentView() = this.setContentView(findViewById(R.layout.layout_24));    ****ERROR****
              {
                  dialog.setContentView(R.layout.popup_24);
              }
              else (this.setContentView() = this.setContentView(findViewById(R.layout.layout_63));    ****ERROR****
              {
                  dialog.setContentView(R.layout.popup_63);
              }
          }
          else 
          {
              finish();
          }
      }
  });

编码或标记中的所有错误。你能帮忙的话,我会很高兴。似乎找不到与此类 if 语句相关的任何信息。

****更新* ******* 新代码仍有错误。在 getBackground 代码上,出现错误:不兼容的操作数类型 Drawable 和 int。在 getContentView 代码上,出现错误:创建方法 getContentView。

Context mContext = getApplicationContext();
final Dialog dialog = new Dialog(mContext);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

final Button quicktab = (Button) findViewById(R.id.btnA4);
quicktab.setOnClickListener(new OnClickListener()
{
  @Override
  public void onClick(View v)
  {
      Button a4 = (Button)findViewById(R.id.btnA4);
      if (a4.getBackground() == R.drawable.quick_btn_background)
      { 
          if (this.getContentView() == R.layout.layout_24)
          {
              dialog.setContentView(R.layout.popup_24);
          }
          else if(this.getContentView() == R.layout.layout_63)
          {
              dialog.setContentView(R.layout.popup_63);
          }
      }
      else finish();

    }
  });

最佳答案

请不要在您的 if 语句中附加分号,并添加一些缺少的右括号,语法应该没问题。像这样:

  Context mContext = getApplicationContext();
  final Dialog dialog = new Dialog(mContext);
  dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

  final Button quicktab = (Button) findViewById(R.id.btnA4);
  quicktab.setOnClickListener(new OnClickListener() 
  {
      @Override
      public void onClick(View v)
      {
          Button a4 = (Button) findViewById(R.id.btnA4);
          if (a4.getBackground() == R.drawable.quick_btn_background)
          { 
              if (this.setContentView() == this.setContentView(findViewById(R.layout.layout_24)))
              {
                  dialog.setContentView(R.layout.popup_24);
              }
              else if(this.setContentView() == this.setContentView(findViewById(R.layout.layout_63))
              {
                  dialog.setContentView(R.layout.popup_63);
              }
              else finish();
          }
    }
});

但是,我完全不确定您是否必须比较 this.setContentView() == this.setContentView(...)。你不是说 getContentView 吗?希望这会有所帮助,祝你好运......

关于android - 带有按钮背景和 Activity 布局的 If 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8196028/

相关文章:

android - 如何获得自定义工具栏上菜单项的涟漪效果?

android - 像 Support NavigationView 中的图标着色

wpf - 半透明自定义布局面板

javascript - 我如何控制来自另一个 html 的元素?

java - 在Java中创建三角形按钮

android - 单击标记从谷歌地图底部滑入 View

Android - 下载 SQL 数据库和搜索

android - 布局何时完成加载

CSS显示三栏内容

Python:点击一个按钮