查找子活动中的查看ID

时间:2017-06-07 06:15:19

标签: android

我有一个问题,需要你的帮助,我的问题是:我有一个多项活动,所以我创建了一个抽象类来保存视图。现在我的问题是我能够将视图传递给基本活动但是如何在子活动类中查找视图ID。

我试过这样:

public abstract class BaseActivity extends Activity {
   @Override
   protected void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(getLayoutResourceId());
   }

   public abstract int getLayoutResourceId();

}
My Activity Class:
    public class ServiceActivity extends BaseActivity {
        Button startSerice_btn, stopService_btn;
        MyService myService;

        @Override
        public int getLayoutResourceId() {
            return R.layout.service_xml;
        }

    }

2 个答案:

答案 0 :(得分:1)

您可以在super.onCreate()

ServiceActivity来电后访问您的观看次数
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Access them here
}

答案 1 :(得分:0)

  

我认为您无需在BaseActivity中设置内容视图,如下所示:

public abstract class MyAppBaseActivity extends AppCompatActivity implements View.OnClickListener {

   @Override
   public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}
  

你只需按照以下方式扩展活动:

    // for MyCustomActivity1 
   public class MyCustomActivity1 extends MyAppBaseActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    public void onClick(View view) {
    }
}


    // for MyCustomActivity2

     public class MyCustomActivity2 extends MyAppBaseActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    public void onClick(View view) {
    }
}