【发布时间】:2015-08-06 02:45:46
【问题描述】:
我正在创建一个 android 应用程序,它需要能够在另一个全局类中的几个方法中在多个 android 活动布局上设置内容视图。因此,当我从新活动 onCreate 调用该方法时,按钮和文本视图由 findviewbyId 通过先前定义的类中的方法分配。示例代码如下所示。
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);
}
});
}
因此,当我从如下所示的活动中调用此方法时,应该执行该方法而不会出现空错误。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
app = (MyApplication)getApplication();
// Call a custom application method
app.executeWorkItem();
}
因此,我应该为此创建一个应用程序类,还是必须创建一个扩展 Activity 的对象并在其中调用该对象方法? 注意:我应该能够创建可以做类似事情的不同方法。
【问题讨论】:
-
为什么你对一堆活动使用相同的布局和视图?
标签: java android findviewbyid