[英]Android: When Button is clicked it will focus and move to other editText [vice-versa]
PS:這只是一個例子,這不是我的實際應用。
我想要的基本上是,如果單擊buttonTwo
,它將移至另一個editText字段,並且再次按下buttonTwo
,它將移回到editText字段(反之亦然)。
因為buttonOne
基本上將要生成“字母”,並且我不想僅單擊editText,所以我希望“如果單擊buttonTwo,它將移至另一個editText(反之亦然)”
我的MainActivity.java
代碼
public class MainActivity extends Activity implements OnClickListener {
String letter;
EditText txtNumber;
EditText txtMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtNumber = (EditText) findViewById(R.id.txtNumber);
txtMessage = (EditText) findViewById(R.id.txtMesssage);
Button buttonOne = (Button) findViewById(R.id.block1);
Button buttonTwo = (Button) findViewById(R.id.block2);
txtNumber = (EditText) findViewById(R.id.txtNumber);
txtMessage = (EditText) findViewById(R.id.txtMesssage);
buttonOne.setOnClickListener(this);
buttonTwo.setOnClickListener(this);
}
public void onClick(View v) {
/* if(txtNumber.requestFocus()) {
txtNumber.setText("");
} else if (buttonTwo.getId() == txtMessage.getId()){
txtMessage.setNextFocusDownId(txtMessage.getId());
txtMessage.setText("");
} */
switch (v.getId()) {
case R.id.block1:
//buttonOnePressed = System.nanoTime(); // assign times
letter += "A";
break;
case R.id.block2:
if(v.getId()==R.id.block2){
if(count%2==0){ //check if button clicked secong time
txtNumber.setSelection(txtNumber.getText().length());
txtNumber.setText(letter);
}
else
{
txtMessage.setSelection(txtMessage.getText().length()); ////when button clicked first time
txtMessage.setText(letter);
}
}
break;
}
}
}
在我的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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/txtNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="0.05"
android:imeOptions="actionNext"
android:hint="Enter Mobile Number" >
<requestFocus>
</requestFocus>
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:orientation="horizontal" >
<Button
android:id="@+id/block1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:text="Button one" />
<Button
android:id="@+id/block2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:text="Button two" />
</LinearLayout>
<EditText
android:id="@+id/txtMesssage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:imeOptions="Done"
android:hint="Enter Message" >
</EditText>
</LinearLayout>
它無法正常運行,但出現錯誤。
您可以在onClick方法中嘗試
if (view.getId() == R.id.block1) {
// generate letters
txtMessage.append("A");
} else if (view.getId() == R.id.block2) {
if (txtNumber.hasFocus()) {
txtMessage.requestFocus();
} else {
txtNumber.requestFocus();
}
}
在我的onClick()
public void onClick(View v) {
if (v.getId() == R.id.block4)
if (txtNumber.hasFocus()) {
txtMessage.requestFocus();
} else {
txtNumber.requestFocus();
}
}
在另一個函數example()
if ( // initiate data ) {
if (txtNumber.hasFocus()){
txtNumber.append(mLookupMap.get(mapKey));
} else if (txtMessage.hasFocus()){
txtMessage.append(mLookupMap.get(mapKey));
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.