主.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spin"
android:entries="@array/num"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="Click ME"
android:gravity="center"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/txtv"
/>
</LinearLayout>
Spin.java
package com.and.spin;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
public class spin extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView tv=(TextView)findViewById(R.id.txtv);
Button b=(Button)findViewById(R.id.btn);
final Spinner s=(Spinner)findViewById(R.id.spin);
b.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String spin=s.toString();
tv.setText(spin);
}
});
}
}
在这个程序中,我试图显示从 Spinner 到 TextView 的选定选项。但输出 dsiplays android.widget.Spinner@44c0d7f8
我希望像 (1,2,3,4 或 5) 这样的输出作为 Spinner 中选择的选项,而不是 android.widget.Spinner@44c0d7f8