Android用户输入,使用自定义键盘

时间:2012-02-21 22:56:08

标签: android android-layout android-edittext android-keypad

首先,任何帮助将不胜感激!我创建了一个数字小键盘,每次选择其中一个按钮时,我都需要将数值传递给EditText。每次按下不同的按钮时,我都会遇到一个问题,其中EditText会被setText覆盖。我真的需要连接每个值,我不太清楚如何做到这一点。可以按任何顺序按下一到九个按钮中的任何一个。

以下是一些代码。我只是想让这些键先工作。

    View hash = findViewById(R.id.keypad_hash);
    hash.setOnClickListener(this);
    View key1 = findViewById(R.id.keypad_1);
    key1.setOnClickListener(this);
    View key2 = findViewById(R.id.keypad_2);
    key2.setOnClickListener(this);

}




@Override
public void onClick(View v){
switch(v.getId()){
case R.id.keypad_hash:
    questions();
    break;

case R.id.keypad_1:

    final EditText number_edit_text1 = (EditText) this.findViewById(R.id.Edit);
      number_edit_text1.setText(String.valueOf("1"));




      break;


case R.id.keypad_2:

    final EditText number_edit_text2 = (EditText) this.findViewById(R.id.Edit);
      number_edit_text2.setText(String.valueOf("2"));
    break;

}   
}

然后是布局中的edittext

<EditText  
android:id="@+id/Edit"  
android:layout_height="wrap_content"    
android:inputType="number"  
android:layout_width="fill_parent"
android:numeric="integer">  
</EditText> 

1 个答案:

答案 0 :(得分:1)

尝试:

number_edit_text2.append(String.valueOf("2"));

如果由于某种原因不起作用:

number_edit_text2.setText(number_edit_text2.getText().toString()+String.valueOf("2"));

另外,在旁注中,您可以更轻松地完成此过程。

在你的xml中,你应该这样做:

android:tag="0"

并将0替换为每个按钮所需的数字。

在您的课堂正文中,您应该声明EditText editText;,然后在onCreate中,您应该editText = (EditText)findViewById(R.id.Edit);

然后在onClick中,执行:

editText.append(String.valueOf(v.getTag()));

这样可以简化您的代码,使其更易于管理,并且可以使用更少的资源,因为您不必重复重新创建EditText

相关问题