0
我想創建一個數字小鍵盤,但是當我點擊任何一個textview時,它不會在EditText中顯示任何數字。我認爲TextView的OnClickListener
事件不起作用。我怎樣才能解決這個問題?無法創建自定義數字小鍵盤
這是我的代碼。
MainActivity.java
public class MainActivity extends Activity {
EditText edtpassword;
TextView tv1;
TextView tv2;
TextView tv3;
TextView tv4;
TextView tv5;
TextView tv6;
TextView tv7;
TextView tv8;
TextView tv9;
TextView tv10;
ImageView iv11;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_keypad);
edtpassword = (EditText)findViewById(R.id.edtpassword);
tv1=(TextView)findViewById(R.id.anti_theft_t9_key_1);
tv2=(TextView)findViewById(R.id.anti_theft_t9_key_2);
tv3=(TextView)findViewById(R.id.anti_theft_t9_key_3);
tv4=(TextView)findViewById(R.id.anti_theft_t9_key_4);
tv5=(TextView)findViewById(R.id.anti_theft_t9_key_5);
tv6=(TextView)findViewById(R.id.anti_theft_t9_key_6);
tv7=(TextView)findViewById(R.id.anti_theft_t9_key_7);
tv8=(TextView)findViewById(R.id.anti_theft_t9_key_8);
tv9=(TextView)findViewById(R.id.anti_theft_t9_key_9);
tv10=(TextView)findViewById(R.id.anti_theft_t9_key_clear);
iv11=(ImageView)findViewById(R.id.anti_theft_t9_key_backspace);
setViews();
}
private void setViews(){
tv1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
onT9KeyClicked(1);
}
});
tv2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
onT9KeyClicked(2);
}
});
tv3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
onT9KeyClicked(3);
}
});
tv4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
onT9KeyClicked(4);
}
});
tv5.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
onT9KeyClicked(5);
}
});
tv6.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
onT9KeyClicked(6);
}
});
tv7.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
onT9KeyClicked(7);
}
});
tv8.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
onT9KeyClicked(8);
}
});
tv9.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
onT9KeyClicked(9);
}
});
tv10.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
onT9KeyClicked(10);
}
});
// find view references...
// set OnClickListener to each key view...
}
private void onT9KeyClicked(int key) {
switch (key) {
case R.id.anti_theft_t9_key_0:
edtpassword.setText(1);
// edtpassword.append("0");
break;
case R.id.anti_theft_t9_key_1:
edtpassword.append("1");
break;
case R.id.anti_theft_t9_key_2:
edtpassword.append("2");
break;
case R.id.anti_theft_t9_key_3:
edtpassword.append("3");
break;
case R.id.anti_theft_t9_key_4:
edtpassword.append("4");
break;
case R.id.anti_theft_t9_key_5:
edtpassword.append("5");
break;
case R.id.anti_theft_t9_key_6:
edtpassword.append("6");
break;
case R.id.anti_theft_t9_key_7:
edtpassword.append("7");
break;
case R.id.anti_theft_t9_key_8:
edtpassword.append("8");
break;
case R.id.anti_theft_t9_key_9:
edtpassword.append("9");
break;
case R.id.anti_theft_t9_key_backspace: {
// delete one character
String passwordStr = edtpassword.getText().toString();
if (passwordStr.length() > 0) {
String newPasswordStr = new StringBuilder(passwordStr)
.deleteCharAt(passwordStr.length() - 1).toString();
edtpassword.setText(newPasswordStr);
}
}
break;
case R.id.anti_theft_t9_key_clear:
// clear password field
edtpassword.setText(null);
break;
}
}
}
這是我的佈局文件。
activity_main.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:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_marginTop="50dp"
android:text="Login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="20dp"
android:textColor="#287AA9"
/>
<TextView
android:layout_marginTop="100dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter Your Password"
android:textColor="#287AA9"
/>
<EditText
android:id="@+id/edtpassword"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/lock2"
android:inputType="number"
/>
<include
android:layout_marginTop="40dp"
android:id="@+id/container_header_lyt"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
layout="@layout/activity_main"
/>
</LinearLayout>
這是我的第二個佈局文件,其中我包含在new_keypad
佈局中。 new_keypad.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/anti_theft_t9_grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="300dp"
android:background="@color/white_gray"
android:divider="@android:color/darker_gray"
android:orientation="vertical"
android:showDividers="middle|beginning|end" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:divider="@android:color/darker_gray"
android:gravity="center"
android:showDividers="middle" >
<TextView
android:id="@+id/anti_theft_t9_key_1"
style="@style/anti_theft_t9_key"
android:layout_weight="1"
android:text="@string/number_one"
android:textIsSelectable="false" />
<TextView
android:id="@+id/anti_theft_t9_key_2"
style="@style/anti_theft_t9_key"
android:layout_weight="1"
android:text="@string/number_two"
android:textIsSelectable="false" />
<TextView
android:id="@+id/anti_theft_t9_key_3"
style="@style/anti_theft_t9_key"
android:layout_weight="1"
android:text="@string/number_three"
android:textIsSelectable="false" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:divider="@android:color/darker_gray"
android:gravity="center"
android:showDividers="middle" >
<TextView
android:id="@+id/anti_theft_t9_key_4"
style="@style/anti_theft_t9_key"
android:layout_weight="1"
android:text="@string/number_four"
android:textIsSelectable="false" />
<TextView
android:id="@+id/anti_theft_t9_key_5"
style="@style/anti_theft_t9_key"
android:layout_weight="1"
android:text="@string/number_five"
android:textIsSelectable="false" />
<TextView
android:id="@+id/anti_theft_t9_key_6"
style="@style/anti_theft_t9_key"
android:layout_weight="1"
android:text="@string/number_six"
android:textIsSelectable="false" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:divider="@android:color/darker_gray"
android:gravity="center"
android:showDividers="middle" >
<TextView
android:id="@+id/anti_theft_t9_key_7"
style="@style/anti_theft_t9_key"
android:layout_weight="1"
android:text="@string/number_seven"
android:textIsSelectable="false" />
<TextView
android:id="@+id/anti_theft_t9_key_8"
style="@style/anti_theft_t9_key"
android:layout_weight="1"
android:text="@string/number_eight"
android:textIsSelectable="false" />
<TextView
android:id="@+id/anti_theft_t9_key_9"
style="@style/anti_theft_t9_key"
android:layout_weight="1"
android:text="@string/number_nine"
android:textIsSelectable="false" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:divider="@android:color/darker_gray"
android:gravity="center"
android:showDividers="middle" >
<TextView
android:id="@+id/anti_theft_t9_key_clear"
style="@style/anti_theft_t9_key"
android:layout_weight="1"
android:text="@string/anti_theft_keyboard_clear"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textIsSelectable="false" />
<TextView
android:id="@+id/anti_theft_t9_key_0"
style="@style/anti_theft_t9_key"
android:layout_weight="1"
android:text="@string/number_zero"
android:textIsSelectable="false" />
<ImageView
android:id="@+id/anti_theft_t9_key_backspace"
style="@style/anti_theft_t9_key"
android:layout_weight="1"
android:src="@drawable/back"
android:textIsSelectable="false" />
</TableRow>
<Button
android:layout_marginTop="10dp"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK" />
</TableLayout>
改變了代碼,但仍然當我按任何數字它不顯示在密碼的textview上。 :( – user3354610 2014-12-13 09:10:19