简体   繁体   English

setContentView来自另一个类的多个活动

[英]setContentView of multiple activities from a another class

I am creating a android application which needs to be able to setcontentview on multiple android activities layouts in few methods in a another global class. 我正在创建一个android应用程序,该应用程序需要能够在另一个全局类中以几种方法在多个android活动布局上设置contentview。 Therefore when I call the method from a new activity onCreate then the buttons and textviews are assigned by findviewbyId by method in previously defined class. 因此,当我从onCreate的新活动中调用方法时,按钮和文本视图由findviewbyId通过先前定义的类中的方法分配。 Example code is shown below. 示例代码如下所示。

   public void executeWorkItem() {
            setContentView(R.layout.activity_message);
            Button button = (Button) findViewById(R.id.btnSendSMS);
            button.setOnClickListener(new Button.OnClickListener() {
                public void onClick(View v) {
                    EditText textPhone = (EditText) findViewById(R.id.phoneNum);
                    EditText textSms = (EditText) findViewById(R.id.smsContent);
                    final String phoneNumber = textPhone.getText().toString();
                    final String message = textSms.getText().toString();
                    Map<String, Object> results = new HashMap<String, Object>();
                    results.put("phoneNmbr", "" + phoneNumber);
                    results.put("message", "" + message);
                }
            });
        }

Therefore when I call this method from activity like shown below then the methhod should be executed without getting null error. 因此,当我从如下所示的活动中调用此方法时,该方法应执行而不会出现空错误。

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

    app = (MyApplication)getApplication();

     // Call a custom application method
    app.executeWorkItem();
} 

Therefore should I create a application class for this or do I have to create a Object which extends Activity and call that object method in there ? 因此,我应该为此创建一个应用程序类,还是必须创建一个扩展Activity的对象并在其中调用该对象方法? Note : that I should be able to create different methods which can do similar things. 注意:我应该能够创建可以做类似事情的不同方法。

Very easy to do. 很容易做到。 All you need is a Parent Activity like a BaseActivity, then all the other activities simple extend that Parent Activity, like so: 您所需要做的只是一个父活动(如BaseActivity),然后所有其他活动简单地扩展了该父活动,如下所示:

public class BaseActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_message);
        Button button = (Button) findViewById(R.id.btnSendSMS);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                EditText textPhone = (EditText) findViewById(R.id.phoneNum);
                EditText textSms = (EditText) findViewById(R.id.smsContent);
                final String phoneNumber = textPhone.getText().toString();
                final String message = textSms.getText().toString();
                Map<String, Object> results = new HashMap<String, Object>();
                results.put("phoneNmbr", "" + phoneNumber);
                results.put("message", "" + message);
            }
        });
    }
}

Then all your other activities extend the Parent Activity like so: 然后,您的所有其他活动将扩展“父活动”,如下所示:

public class MainActivity extends BaseActivity {
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Do not call setContentView again
    }
}

public class SecondActivity extends BaseActivity {
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Do not call setContentView again
    }
}

If you notice the super.onCreate(savedInstanceState) It is simply calling the onCreate method of it's parent class, in this case BaseActivity (which in turn calls Activity class). 如果您注意到super.onCreate(savedInstanceState)它只是在调用其父类的onCreate方法,在本例中为BaseActivity(后者又称为Activity类)。

Not sure I am getting your question fully correct, but perhaps you should create base class for your activities that would wire all common listeners and other things on its onCreate() . 不确定我的问题是否完全正确,但是也许您应该为您的活动创建基类,该基类将所有常见的侦听器和其他内容关联到其onCreate() Your layouts should contain all the buttons base class expects to find (most likely you may want to use <include> or <merge> ( see docs ). Alternatively you can consider using Fragments but that may be more work for you to do. 您的布局应包含基类希望找到的所有按钮(很可能您可能要使用<include><merge>请参阅docs )。或者,您可以考虑使用Fragments但这可能需要做更多的工作。

PS: I do not know what app.executeWorkItem(); PS:我不知道什么app.executeWorkItem(); is doing but are you sure application class is the right place for it? 在做什么,但是您确定应用程序类是正确的地方吗?

EDIT 编辑

I am not sure about that. 我不确定。 I want to handle multiple layouts of multiple activities from single class / activity 我想从单个类/活动处理多个活动的多个布局

This is only matter of how you pass the layoutId to be used. 这仅取决于如何传递要使用的layoutId。 You can make base class abstract with method like abstract int getLayoutId()) and then your base's onCreate() would only need to do setContentView(getLayoutId()); 您可以使用诸如abstract int getLayoutId())类的方法使基类抽象化,然后基类的onCreate()只需要执行setContentView(getLayoutId()); or you can drop base's onCreate() and instead override setContentView()) and do your wiring there (but I do not like that really). 或者您也可以删除base的onCreate()并改写setContentView())并在那里进行连接(但我真的不喜欢)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM