Android的:如何使选择微调项目的TextView? [英] Android: How to make selected spinner item to TextView?
问题描述
我想被选中之后,从微调获得所选择的项目直接显示。试想一下,如果这就像一个问答游戏。如果选择了错误的人之一,敬酒会出现(我不能获得工作)。如果我选择是正确的,不同的TOAST会出现,我将其链接到一个新的活动。我怎样才能获得ONSELECT输出或显示其选择的TextView的,我怎么能实现的,如果合适的语句/错误的答案?
包com.example.test;进口android.os.Bundle;
进口android.app.Activity;
进口android.view.View;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemSelectedListener;
进口android.widget.ArrayAdapter;
进口android.widget.Spinner;
进口android.widget.TextView;公共类MainActivity扩展活动实现OnItemSelectedListener {的String [] test_array;
TextView中显示,选择;
字符串结果;
微调微调;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
微调微调=(微调)findViewById(R.id.spinner);
ArrayAdapter<&CharSequence的GT;适配器= ArrayAdapter.createFromResource(这一点,R.array.test_array,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(适配器);
}@覆盖
公共无效onItemSelected(最终适配器视图<>母公司,观景,最终诠释POS机,
长ID){
// TODO自动生成方法存根
结果=(字符串)spinner.getSelectedItem();
显示=(的TextView)findViewById(R.id.display);
display.setText(结果);
}@覆盖
公共无效onNothingSelected(适配器视图<>为arg0){
// TODO自动生成方法存根}
}
我修改根据Google的试件的SDK。这里是修订code。
包com.example.test;进口android.os.Bundle;
进口android.app.Activity;
进口android.view.View;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemSelectedListener;
进口android.widget.ArrayAdapter;
进口android.widget.Spinner;
进口android.widget.TextView;公共类MainActivity延伸活动{的String [] test_array;
TextView中显示,选择;
字符串结果,S选择;
微调微调;
INT MPOS;
串mSelection;
ArrayAdapter<&CharSequence的GT; mAdapter;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
微调微调=(微调)findViewById(R.id.spinner);
this.mAdapter = ArrayAdapter.createFromResource(这一点,R.array.test_array,android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(this.mAdapter);
OnItemSelectedListener监听器=新myOnItemSelectedListener();
spinner.setOnItemSelectedListener(监听);
}公共类myOnItemSelectedListener实现OnItemSelectedListener { @覆盖
公共无效onItemSelected(适配器视图<>母公司,视图V,INT POS机,
长ID){
// TODO自动生成方法存根
MainActivity.this.mPos = POS;
MainActivity.this.mSelection = parent.getItemAtPosition(POS)的ToString();
TextView的resultText =(的TextView)findViewById(R.id.display);
resultText.setText(MainActivity.this.mSelection);
} @覆盖
公共无效onNothingSelected(适配器视图<>母公司){
// TODO自动生成方法存根 }}
}
TextView的现在改变为选择的项目,现在的我会尝试的if / else。的
在if / else语句会导致崩溃。
一月四日至3日:38:17.648:E / AndroidRuntime(817):致命异常:主要
1月4日至3日:38:17.648:E / AndroidRuntime(817):显示java.lang.NullPointerException
1月4日至3日:38:17.648:E / AndroidRuntime(817):在com.example.test.MainActivity $ myOnItemSelectedListener.onItemSelected(MainActivity.java:43)
1月4日至3日:38:17.648:E / AndroidRuntime(817):在android.widget.AdapterView.fireOnSelected(AdapterView.java:892)
1月4日至3日:38:17.648:E / AndroidRuntime(817):在android.widget.AdapterView.access $ 200(AdapterView.java:49)
1月4日至3日:38:17.648:E / AndroidRuntime(817):在android.widget.AdapterView $ SelectionNotifier.run(AdapterView.java:860)
1月4日至3日:38:17.648:E / AndroidRuntime(817):在android.os.Handler.handleCallback(Handler.java:615)
1月4日至3日:38:17.648:E / AndroidRuntime(817):在android.os.Handler.dispatchMessage(Handler.java:92)
1月4日至3日:38:17.648:E / AndroidRuntime(817):在android.os.Looper.loop(Looper.java:137)
1月4日至3日:38:17.648:E / AndroidRuntime(817):在android.app.ActivityThread.main(ActivityThread.java:4745)
1月4日至3日:38:17.648:E / AndroidRuntime(817):在java.lang.reflect.Method.invokeNative(本机方法)
1月4日至3日:38:17.648:E / AndroidRuntime(817):在java.lang.reflect.Method.invoke(Method.java:511)
1月4日至3日:38:17.648:E / AndroidRuntime(817):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786)
1月4日至3日:38:17.648:E / AndroidRuntime(817):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
1月4日至3日:38:17.648:E / AndroidRuntime(817):在dalvik.system.NativeStart.main(本机方法)
1月4日至3日:39:04.718:E /跟踪(865):错误打开跟踪文件:没有这样的文件或目录(2)
1月4日至3日:39:06.028:D / gralloc_goldfish(865):无仿真模拟GPU检测。
实现code
公共类myOnItemSelectedListener实现OnItemSelectedListener {@覆盖
公共无效onItemSelected(适配器视图<>母公司,视图V,INT POS机,
长ID){
// TODO自动生成方法存根
MainActivity.this.mPos = POS;
MainActivity.this.mSelection = parent.getItemAtPosition(POS)的ToString();
TextView的resultText =(的TextView)findViewById(R.id.display);
resultText.setText(MainActivity.this.mSelection);
如果(result.equalsIgnoreCase(ABC)){
//吐司这里
}其他{
}
}
试试这个
@覆盖
公共无效onItemSelected(适配器视图<取代;父,观景,INT位置,长的id){
//在选择微调项
字符串项= parent.getItemAtPosition(位置)的ToString(); //显示选择微调项目
Toast.makeText(parent.getContext(),选择:+项目,Toast.LENGTH_LONG).show(); }
尝试一下本作的if-else
语句
=微调(微调)findViewById(R.id.test); spinner.setOnItemSelectedListener(新OnItemSelectedListener(){ @覆盖
公共无效onItemSelected(适配器视图<>母公司,观景,
INT位置,长的id){ 字符串项=无;
//在选择微调项
如果(parent.getItemAtPosition(位置)的ToString()。equalsIgnoreCase(小说))
项目= parent.getItemAtPosition(位置)的ToString();
其他{
//做其他事情
} //显示在选择项目举杯
Toast.makeText(parent.getContext()项,Toast.LENGTH_LONG).show();
} @覆盖
公共无效onNothingSelected(适配器视图<>为arg0){
// TODO自动生成方法存根 } });
I am trying to get the selected item from the spinner to be displayed DIRECTLY after it is selected. Imagine if this is like a quiz game. If you select one of the wrong ones, a TOAST will appear(which I cannot get working). If I select the right one, a different TOAST will appear and I will link it to a new activity. How can I get the onselect to output or display its selection to the textview and how can I implement an if statement for the right/wrong answers?
package com.example.test;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends Activity implements OnItemSelectedListener{
String[] test_array;
TextView display, selected;
String result;
Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.test_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
@Override
public void onItemSelected(final AdapterView<?> parent, View view, final int pos,
long id) {
// TODO Auto-generated method stub
result = (String) spinner.getSelectedItem();
display = (TextView) findViewById (R.id.display);
display.setText(result);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
I REVISED ACCORDING TO GOOGLE'S SAMPLES WITHIN THE SDK. HERE IS REVISED CODE.
package com.example.test;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends Activity{
String[] test_array;
TextView display, selected;
String result, sSelect;
Spinner spinner;
int mPos;
String mSelection;
ArrayAdapter<CharSequence> mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
this.mAdapter = ArrayAdapter.createFromResource(this, R.array.test_array, android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(this.mAdapter);
OnItemSelectedListener listener = new myOnItemSelectedListener();
spinner.setOnItemSelectedListener(listener);
}
public class myOnItemSelectedListener implements OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> parent, View v, int pos,
long id) {
// TODO Auto-generated method stub
MainActivity.this.mPos = pos;
MainActivity.this.mSelection = parent.getItemAtPosition(pos).toString();
TextView resultText = (TextView) findViewById(R.id.display);
resultText.setText(MainActivity.this.mSelection);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
}
}
The textview now changes to the selected item, now I will attempt the if/else. The if/else statement causes crash.
04-03 01:38:17.648: E/AndroidRuntime(817): FATAL EXCEPTION: main
04-03 01:38:17.648: E/AndroidRuntime(817): java.lang.NullPointerException
04-03 01:38:17.648: E/AndroidRuntime(817): at com.example.test.MainActivity$myOnItemSelectedListener.onItemSelected(MainActivity.java:43)
04-03 01:38:17.648: E/AndroidRuntime(817): at android.widget.AdapterView.fireOnSelected(AdapterView.java:892)
04-03 01:38:17.648: E/AndroidRuntime(817): at android.widget.AdapterView.access$200(AdapterView.java:49)
04-03 01:38:17.648: E/AndroidRuntime(817): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:860)
04-03 01:38:17.648: E/AndroidRuntime(817): at android.os.Handler.handleCallback(Handler.java:615)
04-03 01:38:17.648: E/AndroidRuntime(817): at android.os.Handler.dispatchMessage(Handler.java:92)
04-03 01:38:17.648: E/AndroidRuntime(817): at android.os.Looper.loop(Looper.java:137)
04-03 01:38:17.648: E/AndroidRuntime(817): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-03 01:38:17.648: E/AndroidRuntime(817): at java.lang.reflect.Method.invokeNative(Native Method)
04-03 01:38:17.648: E/AndroidRuntime(817): at java.lang.reflect.Method.invoke(Method.java:511)
04-03 01:38:17.648: E/AndroidRuntime(817): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-03 01:38:17.648: E/AndroidRuntime(817): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-03 01:38:17.648: E/AndroidRuntime(817): at dalvik.system.NativeStart.main(Native Method)
04-03 01:39:04.718: E/Trace(865): error opening trace file: No such file or directory (2)
04-03 01:39:06.028: D/gralloc_goldfish(865): Emulator without GPU emulation detected.
Implemented code
public class myOnItemSelectedListener implements OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> parent, View v, int pos,
long id) {
// TODO Auto-generated method stub
MainActivity.this.mPos = pos;
MainActivity.this.mSelection = parent.getItemAtPosition(pos).toString();
TextView resultText = (TextView) findViewById(R.id.display);
resultText.setText(MainActivity.this.mSelection);
if(result.equalsIgnoreCase("abc")){
// Toast in here
}else{
}
}
try this
@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();
}
try this for if-else
statement
spinner = (Spinner) findViewById(R.id.test);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String item = "nothing";
// On selecting a spinner item
if(parent.getItemAtPosition(position).toString().equalsIgnoreCase("novels"))
item = parent.getItemAtPosition(position).toString();
else{
//do other things
}
// showing a toast on selecting an item
Toast.makeText(parent.getContext(), item, Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
这篇关于Android的:如何使选择微调项目的TextView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!