源码:package com.ejoyshop.android;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.os.IBinder;
import android.text.InputType;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SimpleAdapter.ViewBinder;
public class FrmManualCodeActivity extends Activity {
private GridView mGridView;
private EditText txtManualinput = null;
String[] appListString = new String[] { "7", "8", "9", "4", "5", "6", "1",
"2", "3", "630", "0", "删除" };
int[] appListID = { 7, 8, 9, 4, 5, 6, 1, 2, 3, -2, 0, -1 };
private boolean istip = false;
private final int MaxInputCount = 16;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.frmmanualcodeactivity);
txtManualinput = (EditText) findViewById(R.id.manual_input_hint_text);
istip = true;
InputMethodManager localInputMethodManager = (InputMethodManager) getSystemService("input_method");
IBinder localIBinder = this.txtManualinput.getWindowToken();
localInputMethodManager.hideSoftInputFromWindow(localIBinder,
InputMethodManager.HIDE_IMPLICIT_ONLY);
mGridView = (GridView) findViewById(R.id.gridview_id);
ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();
int j = appListString.length;
for (int i = 0; i < j; i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("ItemText", appListString[i].toString());
map.put("ItemId", appListID[i]);
lstImageItem.add(map);
}
SimpleAdapter saImageItems = new SimpleAdapter(this, lstImageItem,
R.layout.item_input, new String[] { "ItemText", "ItemId" },
new int[] { R.id.item_text });
mGridView.setAdapter(saImageItems);
mGridView.setOnItemClickListener(new ItemClickListener());
mGridView.setOnItemLongClickListener(new ItemLongClickListener());
txtManualinput.setOnTouchListener(new EditTextTouchListener());
}
class EditTextTouchListener implements View.OnTouchListener {
public boolean onTouch(View v, MotionEvent event) {
int inType = txtManualinput.getInputType();
txtManualinput.setInputType(InputType.TYPE_NULL);
txtManualinput.onTouchEvent(event);
txtManualinput.setInputType(inType);
return true;
}
}
/**
* 按钮的事件
*/
class ItemClickListener implements OnItemClickListener {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String string = txtManualinput.getText().toString();// 文字内容
int j = txtManualinput.getText().toString().length();// 长度
int i = txtManualinput.getSelectionStart();// 0
if (arg2 - 11 == 0) {
if (istip) {
MessageBox.ShowMakeText(FrmManualCodeActivity.this,
"长按\"删除\"可以清空内容!");
istip = false;
}
// 按到删除
if (string.length() > 0 && i - 0 != 0) {
txtManualinput.getText().delete(i - 1, i);
txtManualinput.setSelection(i - 1);
} else {
txtManualinput.setSelection(0);
}
} else {
if (j < MaxInputCount) {
// 按到其他
txtManualinput.getText().insert(i, appListString[arg2]);
txtManualinput.setSelection(i
+ appListString[arg2].length());
} else {
// 长度超过15个字符
}
}
}
}
/**
* 长按删除
*/
class ItemLongClickListener implements OnItemLongClickListener {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if (arg2 - 11 == 0) {
if (txtManualinput.getText().length() > 0) {
txtManualinput.setText("");
}
}
return false;
}
}
@Override
protected void onResume() {
istip = true;
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/loginbgcolor"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@color/black" >
<TextView
android:id="@+id/textView3"
style="@style/TopBarTextStyle"
android:text="@string/manualcode" />
<Button
android:id="@+id/mancode_Btnreturn"
style="@style/TopBtnReturnStyle"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/loginbgcolor"
android:gravity="center"
android:orientation="horizontal"
android:paddingTop="8.0dip" >
<EditText
android:id="@+id/manual_input_hint_text"
android:layout_width="210.0dip"
android:layout_height="43.0dip"
android:layout_marginLeft="15.0dip"
android:layout_weight="1.0"
android:editable="true"
android:hint="@string/manual_input_hint_text"
android:inputType="number"
android:lines="1"
android:maxLength="100" >
<requestFocus />
</EditText>
<ImageView
android:id="@+id/manual_input_search_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5.0dip"
android:layout_marginRight="10.0dip"
android:layout_weight="1.0"
android:src="@drawable/manual_input_bg_selector" />
</LinearLayout>
<LinearLayout
android:id="@+id/grid_id"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="25dip"
android:background="#00000000"
android:gravity="bottom|center" >
<GridView
android:id="@+id/gridview_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/loginbgcolor"
android:fadingEdge="vertical"
android:gravity="center"
android:numColumns="3"
android:stackFromBottom="true"
android:stretchMode="columnWidth" >
</GridView>
</LinearLayout>
</LinearLayout>
gridview的项:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100.0dip"
android:layout_height="50.0dip"
android:background="@drawable/gridview_img_bg"
android:gravity="center"
android:paddingBottom="5.0dip" >
<TextView
android:id="@+id/item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textColor="#ff000000"
android:textSize="20.0dip" />
</LinearLayout>
上面XML layout的背景图

本文介绍了一个Android应用,该应用允许用户通过手动输入和编辑实现特定功能的代码选择,支持快速插入预定义代码片段,并提供了长按删除功能,限制最大输入字符数为16个。

被折叠的 条评论
为什么被折叠?



