在android应用程序中添加图像和搜索选项

在android应用程序中添加图像和搜索选项,android,android-layout,Android,Android Layout,我在时区应用程序中添加搜索选项以列出各个国家或城市时遇到问题。 此外,我想添加一个图像,可以设置为时钟小部件,根据下拉列表中选择的时区显示时间。我不知道从哪里开始,因为我对android应用程序开发非常陌生。任何有助于改进此应用程序的想法都会很有帮助 下面是我试图学习的代码 main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas

我在时区应用程序中添加搜索选项以列出各个国家或城市时遇到问题。 此外,我想添加一个图像,可以设置为时钟小部件,根据下拉列表中选择的时区显示时间。我不知道从哪里开始,因为我对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));
}
}
@覆盖
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
spinnerAvailableID=(微调器)findViewById(R.id.availableID);
textTimeZone=(TextView)findViewById(R.id.timezone);
txtCurrentTime=(TextView)findViewById(R.id.txtCurrentTime);
txtTimeZoneTime=(TextView)findViewById(R.id.txtTimeZoneTime);
字符串[]idArray=TimeZone.getAvailableIDs();
sdf=新的简化格式(“EEEE,dd MMMM yyyy HH:mm:ss”);
idAdapter=newArrayAdapterThis,android.R.layout.simple\u微调器\u项,idArray);
idAdapter.setDropDownViewResource
(android.R.layout.simple\u微调器\u下拉菜单\u项);
spinnerAvailableID.setAdapter(idAdapter);
getGMTTime();
可纺类
.setOnItemSelectedListener(新建
OnItemSelectedListener(){
@凌驾
已选择公共位置(AdapterViewparent、View、int position、longid){
getGMTTime();
字符串selectedId=(字符串)(parent.getItemAtPosition(位置));
时区TimeZone=TimeZone.getTimeZone(selectedId);
字符串TimeZoneName=timezone.getDisplayName();
int TimeZoneOffset=timezone.getRawOffset()/(60*1000);
int hrs=时区偏移/60;
int min=时区偏移%60;
毫秒=毫秒+时区。getRawOffset();
结果日期=新日期(毫秒);
System.out.println(sdf.format(resultdate));
text timezone.setText(时区名称+”:GMT“+小时+”+分钟);
txtTimeZoneTime.setText(“+sdf.format(resultdate));
毫秒=0;
}
@凌驾
未选择任何内容时公共无效(AdapterWebArg0){
}
});
}
私有void getGMTTime(){
当前=Calendar.getInstance();
txtCurrentTime.setText(“+current.getTime());
毫秒=当前。getTimeInMillis();
时区tzCurrent=current.getTimeZone();
int offset=tzCurrent.getRawOffset();
如果(tzCurrent.inDaylightTime(新日期())){
offset=offset+tzCurrent.getdstssavings();
}
毫秒=毫秒-偏移量;
结果日期=新日期(毫秒);
System.out.println(sdf.format(resultdate));
}
}
现在,我在代码中到底在哪里添加搜索栏选项和自定义时钟小部件图像