2014-12-13 3 views
0

숫자 키패드를 만들고 싶지만 텍스트 뷰를 클릭하면 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> 

답변

0

XML에 글고 정의에서이 줄을 삭제하십시오 : 당신이 대신 사용자 정의 키패드 입력을 제공하려는 경우

또한
android:inputType="number" 

, 왜 그것이 글고 어떻게해야합니까 시스템 하나? 당신은 그것을 TextView로 만들어야합니다 :-)

+0

은 코드를 변경했지만 여전히 숫자를 누르면 암호의 textview에 표시되지 않습니다. :( – user3354610

관련 문제