效果图:
menual_search.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="@drawable/dvb_background"
android:gravity="center"
android:orientation="vertical"
android:weightSum="1"
>
<LinearLayout
android:layout_width="760dp"
android:layout_height="450dp"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:background="@drawable/dvb_search_bg"
android:orientation="vertical"
>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="86dp"
android:orientation="vertical"
android:layout_weight="0.14"
>
<TextView
android:id="@+id/textView1"
android:gravity="center"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:textSize="30sp"
android:text="@string/menual_search"
android:textAppearance="?android:attr/textAppearanceLarge"
>
</TextView>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/dvb_title_line"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/fre_layout"
android:layout_width="match_parent"
android:layout_height="56dp"
android:gravity="center"
android:orientation="horizontal"
>
<TextView
android:id="@+id/textView2"
android:layout_width="150dp"
android:layout_height="40dp"
android:gravity="center"
android:text="@string/frequency"
android:textSize="24sp"
android:background="@drawable/dvb_list_item"
>
</TextView>
<EditText
android:id="@+id/menual_search_edit_frq"
android:layout_width="260dp"
android:layout_height="40dp"
android:gravity="center"
android:inputType="numberDecimal"
android:maxLength="6"
android:selectAllOnFocus="true"
android:textColor="#ff0a82e6"
android:textSize="20sp"
android:background="@drawable/text_view_selector"
>
</EditText>
<TextView
android:id="@+id/textView3"
android:layout_width="100dp"
android:layout_height="40dp"
android:gravity="center"
android:textSize="24sp"
android:text="@string/frq_unit"
android:background="@drawable/dvb_list_item"
>
</TextView>
</LinearLayout>
<LinearLayout
android:id="@+id/symbol_layout"
android:layout_width="match_parent"
android:layout_height="56dp"
android:gravity="center"
android:orientation="horizontal"
>
<TextView
android:id="@+id/menualsearchtextView2"
android:layout_width="150dp"
android:layout_height="40dp"
android:gravity="center"
android:text="@string/symbol"
android:textSize="24sp"
android:background="@drawable/dvb_list_item"
>
</TextView>
<EditText
android:id="@+id/menual_search_edit_sym"
android:layout_width="260dp"
android:layout_height="40dp"
android:gravity="center"
android:inputType="numberDecimal"
android:maxLength="4"
android:selectAllOnFocus="true"
android:textColor="#ff0a82e6"
android:textSize="20sp"
android:background="@drawable/text_view_selector"
>
</EditText>
<TextView
android:id="@+id/menualsearchtextView3"
android:textSize="24sp"
android:layout_width="100dp"
android:layout_height="40dp"
android:gravity="center"
android:text="@string/sym_unit"
android:background="@drawable/dvb_list_item"
>
</TextView>
</LinearLayout>
<LinearLayout
android:id="@+id/qam_layout"
android:layout_width="match_parent"
android:layout_height="56dp"
android:gravity="center"
android:orientation="horizontal"
>
<TextView
android:id="@+id/menualsearchtextview2"
android:layout_width="150dp"
android:layout_height="40dp"
android:gravity="center"
android:text="@string/modulation"
android:textSize="24sp"
android:background="@drawable/dvb_list_item"
>
</TextView>
<Spinner
android:id="@+id/menual_search_edit_mod"
android:layout_width="260dp"
android:layout_height="40dp"
style="@style/dvb_spinner"
>
</Spinner>
<TextView
android:textSize="24sp"
android:layout_width="100dp"
android:layout_height="40dp"
android:gravity="center"
android:text="@string/qam_unit"
android:background="@drawable/dvb_list_item"
>
</TextView>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="70dp"
android:gravity="center"
android:orientation="vertical"
android:layout_weight="0.51"
>
<Button
android:id="@+id/menual_search_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|bottom"
android:background="@drawable/ok_btn"
android:text="@string/ok"
android:textSize="24sp" >
<requestFocus></requestFocus>
</Button>
</LinearLayout>
</LinearLayout>
</LinearLayout>
其中 Spinner 的style style="@style/dvb_spinner"
<style name="dvb_spinner" parent="@android:style/Widget.Spinner">
<item name="android:background">@drawable/spinner_item_selector</item>
<item name="android:clickable">true</item>
<item name="android:spinnerMode">dropdown</item>
<item name="android:dropDownSelector">@drawable/list_selector</item>
<item name="android:popupBackground">@drawable/popup_selector</item>
<item name="android:dropDownVerticalOffset">0dip</item>
<item name="android:dropDownHorizontalOffset">0dip</item>
<item name="android:dropDownWidth">wrap_content</item>
<item name="android:gravity">center</item>
</style>
spinner_item_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:state_window_focused="false"
android:state_enabled="true"
android:drawable="@drawable/spinner_item_bg"
>
</item>
<item
android:state_focused="true"
android:drawable="@drawable/spinner_item_selected"
>
</item>
<item
android:drawable="@drawable/spinner_item_bg"
>
</item>
</selector>
list_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:state_selected="true"
android:drawable="@drawable/edit_text_fg"
>
</item>
<item
android:drawable="@color/full_transparent_background"
>
</item>
</selector>
<color name="full_transparent_background">#00000000</color>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:state_above_anchor="true"
android:drawable="@drawable/text_selected"
>
</item>
<item
android:drawable="@drawable/text_selected"
>
</item>
</selector>
MenualSearch.java
package com.um.ui;
import java.util.ArrayList;
import java.util.List;
import com.um.controller.AppBaseActivity;
import com.um.controller.ParamSave;
import com.um.dvbsearch.R;
import android.app.Activity;
import android.app.Instrumentation;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class MenualSearch extends AppBaseActivity {
protected static final String TAG = "MenualSearch";
private Spinner qam_spinner;
private EditText fre_ed;
private EditText smbl_ed;
private Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menual_search);
qam_spinner = (Spinner)findViewById(R.id.menual_search_edit_mod);
// ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
// this, R.array.qam_array, R.layout.simple_spinner_item);
List<CharSequence> spinnerVal = new ArrayList<CharSequence>();
spinnerVal.add("16");
spinnerVal.add("32");
spinnerVal.add("64");
spinnerVal.add("128");
spinnerVal.add("256");
ArrayAdapter<CharSequence> adapter =
new ArrayAdapter<CharSequence>(MenualSearch.this, R.layout.simple_spinner_item,spinnerVal ){
@Override
public View getDropDownView(int position, View convertView, android.view.ViewGroup parent) {
parent.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
// TODO Auto-generated method stub
if(arg2.getAction() == KeyEvent.ACTION_DOWN){
Log.i(TAG,"keycode="+arg1);
switch (arg1) {
case KeyEvent.KEYCODE_MENU:
Log.i(TAG, "parent KEYCODE_MENU is click");
new Thread() {
public void run() {
try {
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
} catch (Exception e) {
Log.i(TAG,"Exception when sendKeyDownUpSync e="+e.toString());
}
}
}.start();
break;
case KeyEvent.KEY_SOURCEENTER:
Log.i(TAG, "parent KEY_SOURCEENTER is click");
new Thread() {
public void run() {
try {
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER);
} catch (Exception e) {
Log.i(TAG,"Exception when sendKeyDownUpSync e="+e.toString());
}
}
}.start();
return true;
}
}
return false;
}
});
return super.getDropDownView(position, convertView, parent);
}
};
adapter.setDropDownViewResource(R.layout.simple_spinner_item);
qam_spinner.setAdapter(adapter);
qam_spinner.setSelection(2);
fre_ed = (EditText)findViewById(R.id.menual_search_edit_frq);
smbl_ed= (EditText)findViewById(R.id.menual_search_edit_sym);
fre_ed.setText("227000");
smbl_ed.setText("6875");
fre_ed.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
int freqVal;
if(hasFocus){//��������ý���
}else{
if(0 != Integer.parseInt(fre_ed.getText().toString())%10){
Toast.makeText(MenualSearch.this, getResources().getString(R.string.lastNumMayNotBeNonzero), 1).show();
freqVal = Integer.parseInt(fre_ed.getText().toString());
freqVal = freqVal/10*10;
fre_ed.setText(Integer.toString(freqVal));
}
}
}
});
btn = (Button)findViewById(R.id.menual_search_button);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
btnClick();
}
});
}
private void btnClick() {
if(fre_ed.getText().toString().equals("")){
fre_ed.requestFocus();
Toast.makeText(MenualSearch.this, getResources().getString(R.string.inputNotEmpty), 1).show();
return ;
}else if(smbl_ed.getText().toString().equals("")){
smbl_ed.requestFocus();
Toast.makeText(MenualSearch.this, getResources().getString(R.string.inputNotEmpty), 1).show();
return ;
}
int fre;
int symbl;
int qam;
int type = 1;
fre = Integer.parseInt(fre_ed.getText().toString());
symbl = Integer.parseInt(smbl_ed.getText().toString());
qam = qam_spinner.getSelectedItemPosition()+1;
Log.i("MenualSearch",""+fre +symbl +qam);
Intent it = new Intent(MenualSearch.this, Search.class);
Bundle bundle = new Bundle();
bundle.putInt("type", type);
bundle.putInt("fre", fre);
bundle.putInt("sym", symbl);
bundle.putInt("qam", qam);
it.putExtras(bundle);
Bundle extras = getIntent().getExtras();
if (extras != null) {
it.putExtras(extras);
}
startActivity(it);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_MENU:
new Thread() {
public void run() {
try {
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
} catch (Exception e) {
Log.i(TAG,"Exception when sendKeyDownUpSync e="+e.toString());
}
}
}.start();
break;
case KeyEvent.KEY_SOURCEENTER:
if(fre_ed.isFocused()||smbl_ed.isFocused()){
Log.i(TAG, "isFocused KEY_SOURCEENTER is click");
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
if(btn.isFocused()){
btnClick();
}
if(qam_spinner.isFocused()){
new Thread() {
public void run() {
try {
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER);
} catch (Exception e) {
Log.i(TAG,"Exception when sendKeyDownUpSync e="+e.toString());
}
}
}.start();
}
return true;
}
return super.onKeyDown(keyCode, event);
}
}
图片资源文件:链接:http://pan.baidu.com/s/1gexMTcn 密码:ei7h
备注:
我的网盘/mycode/DvbSearch_Spinner _code.rar