android-根据另一个微调框选择更改微调框
作者:互联网
我有包含所有国家/地区名称的字符串数组,以及另一个包含相应国家/地区代码的字符串数组.
我将国家/地区名称字符串设置为spinner.
当我从微调框选择项目时,应将其相应的国家/地区代码设置为微调框.该项目也应更改为Code.
同样,如果我选择其他项目,则其代码也应设置为微调框和项目.但是我选择的第一个应该更改为其原始国家名称.
我应该怎么做?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.temp.MainActivity" >
<Spinner
android:id="@+id/spinnerCountry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="124dp" />
</RelativeLayout>
Java代码
package com.example.temp;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class MainActivity extends Activity {
final String[] countries = new String[] {"Afghanistan(AF)", "Albania(AX)",
"Algeria(AL)", "American Samoa(DZ)",
"Andorra(AS)"};
final String[] countrycodes = new String[]{"AF","AX","AL","DZ","AS"};
Spinner spinnerCountry;
ArrayAdapter<String> arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinnerCountry=(Spinner)findViewById(R.id.spinnerCountry);
arrayAdapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1,countries);
spinnerCountry.setAdapter(arrayAdapter);
}
}
我无法在我的应用程序中使用原始照片时将其共享.
但是,我正在共享与原始代码相同的代码.
从微调框选择国家时,我想设置相应的代码.
解决方法:
String[] countries = {"INDIA","DUBAI","KSA"};
String[] country_code = {"91","971","966"};
final Spinner spinnerCountry = (Spinner)findViewById (R.id.spinnerCountry);
final Spinner spinnerCountryCode = (Spinner) findViewById (R.id.spinnerCountryCode);
ArrayAdapter<String> countryAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,countries);
ArrayAdapter<String> countryCodeAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,country_code);
spinnerCountry.setAdapter(countryAdapter);
spinnerCountryCode.setAdapter(countryCodeAdapter);
spinnerCountry.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long arg3) {
spinnerCountryCode.setSelection(position);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
spinnerCountryCode.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long arg3) {
spinnerCountry.setSelection(position);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
这是一些可以解决您问题的代码.
说明:
假设您有两个微调器R.id.spinnerCountry和R.id.spinnerCountryCode.
如您所见,我创建了两个分别包含countryName和countryCode的数组.
工作流程:
当我在微调器中选择一项时,其他微调器会加载与该位置相对应的另一项.
代码是自解释的!
标签:android,android-spinner 来源: https://codeday.me/bug/20191009/1881888.html