当这个过程正常工作时,我想制作一个按钮来保存/打印/显示2个微调器中的名称。
执行用户名时将按下此按钮。 例如:
当项目==琳达时,琳达按下了按钮。直接按钮将在spinner1上显示/打印Linda的名字。
其他用户也会遇到同样的情况。并且名称将直接保存在后续的微调器(Spinner2)中。
这是我的代码......
MainProgram(主程序)
此主程序系统用于循环名称。
public boolean onOptionsItemSelected(MenuItem item) {
Log.i(TAG, "Menu Item selected " + item);
if (item == Linda) {
variabel.setViewMode(ClassOne.Object1);
new Thread(new Runnable(){
public void run() {
try {
Thread.sleep(5000);
onOptionsItemSelected(David);}
catch (Exception ex){}}
}).start();
} else if (item == David) {
variabel.setViewMode(ClassOne.Object2);
new Thread(new Runnable(){
public void run() {
try {
Thread.sleep(5000);
onOptionsItemSelected(Samuel);}
catch (Exception ex){}}
}).start();
} else if (item == Samuel) {
variabel.setViewMode(ClassOne.Object3);
new Thread(new Runnable(){
public void run() {
try {
Thread.sleep(5000);
onOptionsItemSelected(Linda);}
catch (Exception ex){}}
}).start();
}
SpinnerProgram
这是我的代码......
private Spinner spinner1, spinner2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
addListenerOnSpinnerItemSelection();
}
public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner2.setOnItemSelectedListener(new CustomOnItemSelectedListener());
// get the selected dropdown list value
public void addListenerOnButton() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
public void onClick(View v) {
/* get names when button stop on looping process and print the names in spinners*/
String name1 = String.valueOf(spinner1.getSelectedItem());
String name2 = String.valueOf(spinner2.getSelectedItem());
String print_name = getName(name1);
String print_name = getName(name2);
public String getName(String name) {
String value = "0";
if (name.equalsIgnoreCase("Linda")) {
value = "0";
} else if (name.equalsIgnoreCase("David")) {
value = "1";
} else if (name.equalsIgnoreCase("Samuel")) {
value = "2"; }
STRING.XML
<string-array name="names">
<item>Linda</item>
<item>David</item>
<item>Samuel</item>
我的问题:
如何创建按钮,我应该在哪里放置可以直接在旋转器中打印/显示所选名称的按钮?
答案 0 :(得分:0)
我不确定这正是你想要的,但也许它至少可以帮助我通过你的评论弄明白。
main.xml 布局:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Spinner android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:entries="@array/names" />
<Spinner android:id="@+id/spinner2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:entries="@array/names" />
</LinearLayout>
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choose" />
</LinearLayout>
<强> MainActivity 强>
public class MainActivity extends Activity implements OnClickListener
{
private Button button;
private Spinner spinner1, spinner2;
private long startTime = 0;
private int elapsed = 0;
private final int TIMEOUT = 3;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.button);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
button.setOnClickListener(this);
startTime = System.currentTimeMillis();
handler.postDelayed(run, 0);
}
@Override
public void onClick(View v)
{
handler.removeCallbacks(run);
spinner2.setSelection(spinner1.getSelectedItemPosition());
}
final Handler handler = new Handler();
Runnable run = new Runnable()
{
@Override
public void run()
{
long millis = System.currentTimeMillis() - startTime;
int seconds = (int) (millis / 1000);
seconds = seconds % 60;
elapsed = TIMEOUT - seconds;
if (elapsed == 0)
{
incrementSpinner();
startTime = System.currentTimeMillis();
handler.postDelayed(this, 0);
}
else
{
handler.postDelayed(this, 500);
}
}
};
private void incrementSpinner()
{
int selPos = spinner1.getSelectedItemPosition();
if(selPos == spinner1.getCount() - 1)
{
selPos = 0;
}
else
{
selPos++;
}
spinner1.setSelection(selPos);
}
}