2014-10-11 92 views
0

我有一个微调框,我希望微调框选中的项目进入旁边的文本视图。这是我的代码到目前为止:将内容从微调框改为文本视图

public class MainActivity extends Activity { 

    Spinner sp1; 
    TextView txt; 

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

     sp1 = (Spinner) findViewById(R.id.spinner1); 
     txt = (TextView) findViewById(R.id.textView1); 

     sp1.setOnItemSelectedListener(
      new OnItemSelectedListener() { 
       public void onItemSelected(
        AdapterView<?> parent, View view, int position, long id) { 

         String val = sp1.getSelectedItem().toString(); 
         txt.setText(val); 
        } 

        public void onNothingSelected(AdapterView<?> parent) { 

        } 
       }); 
    } 
+0

此代码的工作? – Jaguar 2014-10-11 20:02:29

+0

是的,但它只是将项目放在微调器的顶部,而不是在文本视图中我不知道如何做得更多 – 2014-10-11 20:09:24

回答

0

你应该也发布xml代码的布局。检查布局中的TextView在哪里(图形布局)。下面 见代码:

布局

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<Spinner 
    android:id="@+id/spnr" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

<TextView 
    android:id="@+id/textview" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Text" 
    android:layout_below="@+id/spnr"/> 

</RelativeLayout> 

代码