gpt4 book ai didi

android - 触摸 TextField 时打开 Spinner

转载 作者:行者123 更新时间:2023-11-29 16:22:53 25 4
gpt4 key购买 nike

我有一个 EditText 和一个微调器,我想在我关注 Edittext 时打开微调器。如何在android中做到这一点。?请帮忙。

最佳答案

OnFocusChange 监听器附加到 editText。当edittext获得焦点时调用spinner的performClick()方法打开spinner

例子

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.telephony.TelephonyManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.format.DateFormat;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class Main extends Activity{
EditText tv = null;
Button btn = null;
Handler h;
Spinner s;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

h = new Handler();

s = (Spinner) findViewById(R.id.spinner);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
R.array.planets, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);
EditText et = null;
et.setOnFocusChangeListener(new OnFocusChangeListener() {

@Override
public void onFocusChange(View arg0, boolean hasFocus) {
// TODO Auto-generated method stub
if(hasFocus){
s.performClick();
}
}
});
// Open the Spinner...


// Spawn a thread that triggers the Spinner to open after 5 seconds...
// new Thread(new Runnable() {
// public void run() {
// // DO NOT ATTEMPT TO DIRECTLY UPDATE THE UI HERE, IT WON'T WORK!
// // YOU MUST POST THE WORK TO THE UI THREAD'S HANDLER
// h.postDelayed(new Runnable() {
// public void run() {
// // Open the Spinner...
// s.performClick();
// }
// }, 5000);
// }
// }).start();
}
}

关于android - 触摸 TextField 时打开 Spinner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6449520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com