2017-10-16 98 views
0

這是主要的XML:如何在textview中設置國家/地區代碼以在微調器中選擇國家/地區名稱?

<Spinner 
     android:id="@+id/spinner" 
     android:layout_width="wrap_content" 
     android:layout_height="40sp" 
     android:layout_marginTop="37dp" 
     android:layout_below="@+id/textView2" 
     android:layout_toLeftOf="@+id/button" 
     android:layout_toStartOf="@+id/button" 
     android:layout_marginRight="29dp" 
     android:layout_marginEnd="29dp" /> 

    <TextView 
     android:id="@+id/countrycode" 
     android:layout_width="wrap_content" 
     android:text="+" 
     android:textSize="30sp" 
     android:layout_height="40sp" 
     android:ems="10" 
     android:layout_alignTop="@+id/spinner" 
     android:layout_toLeftOf="@+id/editText2" 
     android:layout_toStartOf="@+id/editText2" /> 

這是我的Java代碼:

import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Spinner; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.util.ArrayList; 
import java.util.List; 

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener { 
    TextView textview; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     TextView textview = (TextView) findViewById(R.id.countrycode); 

     // Spinner element 
     Spinner spinner = (Spinner) findViewById(R.id.spinner); 

     // Spinner click listener 
     spinner.setOnItemSelectedListener(this); 

     // Spinner Drop down elements 
     String[] countryname = {"India", "America", "Japan", "Austrailia", "Canada" }; 

     // Creating adapter for spinner 
     ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, countryname); 

     // Drop down layout style - list view with radio button 
     dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

     // attaching data adapter to spinner 
     spinner.setAdapter(dataAdapter); 
    } 

    @Override 
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
     // On selecting a spinner item 
     String item = parent.getItemAtPosition(position).toString(); 


//  // Showing selected spinner item 
     Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show(); 
    } 
    public void onNothingSelected(AdapterView<?> arg0) { 
     // TODO Auto-generated method stub 

    } 
} 

這是我的代碼。如何在TextView中設置國家/地區代碼以在Spinner中選擇國家/地區名稱?我想使用這段代碼,因爲它對我來說很容易理解,而且它工作正常,但我不能做我想做的事。

我想如果我選擇印度在微調那麼我得到印度ISD代碼(+91)是TextView。

+0

您希望您的微調控制器根據您在TextView中輸入的國家/地區代碼顯示國家/地區名稱? – nhoxbypass

+0

它正在工作,但我不能做我想要的東西?那麼它不適合你。 據我可以理解你的問題,你想從微調選擇國家,並選擇國家,textView得到更新國家代碼的權利? 那麼它有什麼問題呢?看起來坦率而且容易解決。 –

回答

0

設置一個TextWatcher你的TextView:

textview.addTextChangeListener(new TextWatcher());和實施onTextChange()方法從用戶鍵入的國家代碼,然後把在SWICH /箱,以確定我應該選擇哪個國家的名字。

0

使用散列表。

class MainActivity { 

.... 
    Hashtable countryCodes<String, String>; 
    TextView textViewCountryCode; 
.... 

    onCreate(....){ 
     .... 
     textViewCountryCode = (TextView) findViewById(R.id.countrycode); 
     countryCodes = new Hashtable<>(); 
     countryCodes.put("India", "+91"); 
     .... 

    } 
.... 
    onItemSelected(....){ 
     .... 
     String countryCode = countryCodes.get(item); 
     textViewCountryCode.setText(countryCode); 
     .... 
    } 
} 
-1

我的理解是你想在你提供的代碼中得到答案。所以你可以做的是採取更多的國家代碼數組和On微調onSelected方法將選定的位置設置爲文本視圖,但一定要保持國名和國家代碼索引相同。或者我會建議使用HashMap相同。

//在創建之前在下面添加以下內容 TextView textview; String [] countrycode = {「+91」,「+1」,「+81」,「+61」,「+1」};

和onItemSelected()方法執行此

textview.setText(COUNTRYCODE [位置]);

相關問題