触摸TextField时打开Spinner

时间:2011-06-23 04:59:32

标签: android

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

2 个答案:

答案 0 :(得分:3)

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

实施例

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();
}
}

答案 1 :(得分:1)

我遇到了同样的问题。我做的是,我使用按钮而不是 EditText 按钮 onClick 我显示了 Spinner 。所选的 Spinner 值会在按钮上显示为文本。我希望这不会成为问题。如果你想要代码,请告诉我 要点击 EditText ,请参阅this发布。