版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/I123456789T/article/details/89352174
先看效果:
1、先自定义个背景图片:
bg_num.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#fff"/>
<corners android:radius="8dp"/>
<stroke android:width="1dp"
android:color="#d6d6d6"/>
</shape>
2、布局代码:number_bord.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:background="@drawable/bg_num"
android:orientation="vertical"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="20dp"
android:paddingRight="20dp">
<!--<android.support.v7.widget.RecyclerView-->
<!--android:id="@+id/gridview"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent"-->
<!--android:visibility="gone"/>-->
<GridView
android:id="@+id/gridList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="3"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"></GridView>
</LinearLayout>
</LinearLayout>
3、activity 代码:
package com.example.weiwenyi.androidtest.input_view;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;
import com.example.weiwenyi.androidtest.R;
import com.example.weiwenyi.androidtest.adapter.TestNumberAdapter;
import java.util.ArrayList;
import java.util.List;
public class TestInputActivity extends AppCompatActivity implements TestNumberAdapter.OnClickItemListener {
List<String> numList;
private GridView gridList;
TextView edit;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nunber_bord);
gridList = findViewById(R.id.gridList);
edit = findViewById(R.id.edit);
addData();
final TestNumberAdapter adapter1 = new TestNumberAdapter(numList,this);
gridList.setAdapter(adapter1);
adapter1.setItemListener(this);
edit.addTextChangedListener(new TextWatch());
// gridList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
// @Override
// public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// view.setOnKeyListener(new View.OnKeyListener() {
// @Override
// public boolean onKey(View v, int keyCode, KeyEvent event) {
// switch (event.getAction()){
// case MotionEvent.ACTION_POINTER_UP:
// Log.d("Testinput","---------->长按了 ");
// adapter1.clearBg();
// break;
// }
// return false;
// }
// });
// }
// });
}
class TextWatch implements TextWatcher {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
}
private void addData() {
numList = new ArrayList<>();
for (int i=0;i<12;i++){
if (i < 9)
numList.add((i+1) + "");
else if (i == 9){
numList.add("清空");
}else if (i == 10){
numList.add("0");
}else if (i == 11) numList.add("删除");
}
}
@Override
public void setOnUpItemClick(int position) {
if (position == 9){
edit.setText("");
}else if (position == 11){
String currentClear = edit.getText().toString().trim();
Log.i("SecurityCodeView","----------抬起----> " + currentClear.length());
String currentContent = currentClear.substring(0,currentClear.length()-1);
edit.setText(currentContent);
}else {
String con = numList.get(position);
String currentC = edit.getText().toString().trim();
edit.setText(currentC + con);
}
}
@Override
public void setOnDownItemClick(int position) {
String currentC = edit.getText().toString().trim();
}
}
运行即可看到,