【问题标题】:content from a spinner to a text view从微调器到文本视图的内容
【发布时间】:2014-10-11 19:58:16
【问题描述】:

我有一个微调器,我希望微调器选择的项目进入它旁边的文本视图。到目前为止,这是我的代码:

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) {

                    }
               });
    }

【问题讨论】:

  • 这段代码有效吗?
  • 是的,但它只是将项目放在微调器的顶部而不是在文本视图中我不知道如何做更多

标签: java android textview android-spinner


【解决方案1】:

您还应该发布布局的 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>

代码

public class MainActivity extends Activity {
Spinner spinner;
TextView textView;
String[] items = { "Sunday", "Monday", "Tuesday" };
ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_main);

    spinner = (Spinner) findViewById(R.id.spnr);
    textView = (TextView) findViewById(R.id.textview);

    adapter = new ArrayAdapter<String>(MainActivity.this,
            android.R.layout.simple_list_item_1, items);
    spinner.setAdapter(adapter);

    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub
            textView.setText(spinner.getSelectedItem().toString()); 
            // You can use your array also:
            // textView.setText(items[position]);   
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub

        }
    });
}

}

【讨论】:

    猜你喜欢
    • 2014-03-09
    • 1970-01-01
    • 2012-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多