所以我对android编程很新,并且没有真正的软件构建经验,但是我确实有PHP的基本背景,所以我得到了一些理论背后的所有。
我目前正在制作基本的表单活动,将条目存储到数据库中,可以在另一个可以查看和编辑的活动中调用等等。
我找到了很多有用的建议,但我目前在构建代码以实现DatePicker对话框时遇到问题,我面临的问题是日期选择器的示例代码< / p>
public class MainActivity extends Activity implements OnClickListener {
我的项目中的代码是public class New extends ActionBarActivity {
因此,当我将该行编辑为“实现OnClickListener”时,我的微调器下拉不喜欢它。
我尝试在没有“implements OnClickListener”的情况下输入代码
请看一下我所拥有的并指出正确的方向,我需要了解它出错的地方以及应该如何构建。
public class New extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
//产品清单
Spinner productList = (Spinner) findViewById(R.id.product);
String[] products = new String[] { "1", "2", "3" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, products);
productList.setAdapter(adapter);
productList.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Log.v("products", (String) parent.getItemAtPosition(position));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
//原因列表
Spinner reasonList = (Spinner) findViewById(R.id.reason);
String[] reasons = new String[] { "A", "B", "C" };
ArrayAdapter<String> adapters = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, reasons);
reasonList.setAdapter(adapters);
reasonList.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Log.v("reasons", (String) parent.getItemAtPosition(position));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
//开始日期选择器
private EditText StartDate;
private DatePickerDialog StartdatePickerDialog;
private SimpleDateFormat dateFormatter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
findViewsById();
setDateTimeField();
}
private void findViewsById() {
StartDate = (EditText) findViewById(R.id.startdate);
StartDate.setInputType(InputType.TYPE_NULL);
StartDate.requestFocus();
}
private void setDateTimeField() {
StartDate.setOnClickListener(this);
Calendar newCalendar = Calendar.getInstance();
StartdatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
StartDate.setText(dateFormatter.format(newDate.getTime()));
}
}, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
}
@Override
public void onClick(View view) {
if(view == StartDate) {
StartDatePickerDialog.show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_new, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:1)
除非我弄错了,请尝试从您的活动中移除implements OnClickListener
并替换
StartDate.setOnClickListener(this);
带
StartDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(view == StartDate) {
StartDatePickerDialog.show();
}
}
});
并删除“开始日期选择器”中的onclick
方法。基本上,implements OnClickListener
所做的是它可以附加到组件(在这种情况下为EditText
),当单击时,它将被重定向到onClick
的实现。您已将Activity
侦听器(可通过setOnClickListener(this)
使用该活动中的任何组件)替换为仅为StartDate
设计的特定侦听器。请注意你的微调器都有onItemSelectedListeners
,它们的工作原理基本相同。
言语很难,如果这没有意义,请告诉我。