我无法添加搜索选项以列出我的时区应用中的各个国家/地区或城市。 我还想添加一个可以设置为时钟小部件的图像,根据从下拉列表中选择的时区显示时间..我不知道从哪里开始,因为我对Android应用程序开发很新。帮助改进此应用程序的想法会有所帮助..
这是我想要学习的代码。
main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|top"
android:orientation="vertical"
android:padding="5dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/txtGMTTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/current" />
<TextView
android:id="@+id/txtCurrentTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity" />
</LinearLayout>
<Spinner
android:id="@+id/availableID"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/timezone"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/txtTimeZoneTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
后跟java类代码。
MainActivity.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
spinnerAvailableID = (Spinner) findViewById(R.id.availableID);
textTimeZone = (TextView) findViewById(R.id.timezone);
txtCurrentTime = (TextView) findViewById(R.id.txtCurrentTime);
txtTimeZoneTime = (TextView) findViewById(R.id.txtTimeZoneTime);
String[] idArray = TimeZone.getAvailableIDs();
sdf = new SimpleDateFormat("EEEE, dd MMMM yyyy HH:mm:ss");
idAdapter = new ArrayAdapter<String>this,android.R.layout.simple_spinner_item,idArray);
idAdapter.setDropDownViewResource
(android.R.layout.simple_spinner_dropdown_item);
spinnerAvailableID.setAdapter(idAdapter);
getGMTTime();
spinnerAvailableID
.setOnItemSelectedListener(new
OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?>parent,View view, int position, longid) {
getGMTTime();
String selectedId = (String) (parent.getItemAtPosition(position));
TimeZone timezone =TimeZone.getTimeZone(selectedId);
String TimeZoneName =timezone.getDisplayName();
int TimeZoneOffset =timezone.getRawOffset()/ (60 * 1000);
int hrs = TimeZoneOffset / 60;
int mins = TimeZoneOffset % 60;
miliSeconds = miliSeconds +timezone.getRawOffset();
resultdate = new Date(miliSeconds);
System.out.println(sdf.format(resultdate));
textTimeZone.setText(TimeZoneName + ": GMT " + hrs + "."+ mins);
txtTimeZoneTime.setText("" +sdf.format(resultdate));
miliSeconds = 0;
}
@Override
public void onNothingSelected(AdapterView<?>arg0) {
}
});
}
private void getGMTTime() {
current = Calendar.getInstance();
txtCurrentTime.setText("" + current.getTime());
miliSeconds = current.getTimeInMillis();
TimeZone tzCurrent = current.getTimeZone();
int offset = tzCurrent.getRawOffset();
if (tzCurrent.inDaylightTime(new Date())) {
offset = offset + tzCurrent.getDSTSavings();
}
miliSeconds = miliSeconds - offset;
resultdate = new Date(miliSeconds);
System.out.println(sdf.format(resultdate));
}
}
现在我在这段代码中添加搜索栏选项和自定义时钟小部件图片