要達到Seekbar、EditText雙向控制其實很簡單
首先要做到以下兩件事情:
1,EditText輸入完後按下Enter鍵要傳值給Seekbar
2,EditText要顯示Seekbar的值
通常EditText在輸入時,虛擬鍵盤上的Enter鍵都預設為"換行",如果要將Enter鍵改成其他功能,就必須在activity_main.xml中的EditText標籤使用android:imeOptions這個屬性,並且將EditText設為單行輸入(加上android:singleLine="true")
其中,android:imeOptions屬性的種類有:
actionUnspecified 未指定
actionNone 無動作
actionGo 前往
actionSearch 搜尋
actionSend 發送
actionNext 下一個
actionDone 完成
然後在MainActivity.java中,將EditText加上setOnEditorActionListener事件,就可以讓Seekbar取得EditText的值了
而要讓EditText取得Seekar的值就需要在MainActivity.java中加上setOnSeekBarChangeListener的事件
例如:
執行結果:
參考來源:Lutas
首先要做到以下兩件事情:
1,EditText輸入完後按下Enter鍵要傳值給Seekbar
2,EditText要顯示Seekbar的值
通常EditText在輸入時,虛擬鍵盤上的Enter鍵都預設為"換行",如果要將Enter鍵改成其他功能,就必須在activity_main.xml中的EditText標籤使用android:imeOptions這個屬性,並且將EditText設為單行輸入(加上android:singleLine="true")
其中,android:imeOptions屬性的種類有:
actionUnspecified 未指定
actionNone 無動作
actionGo 前往
actionSearch 搜尋
actionSend 發送
actionNext 下一個
actionDone 完成
然後在MainActivity.java中,將EditText加上setOnEditorActionListener事件,就可以讓Seekbar取得EditText的值了
而要讓EditText取得Seekar的值就需要在MainActivity.java中加上setOnSeekBarChangeListener的事件
例如:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_marginTop="32dp" | |
android:layout_marginLeft="16dp" | |
android:layout_marginRight="16dp" | |
tools:context="com.example.edittextdemo.MainActivity" | |
android:orientation="horizontal"> | |
<SeekBar | |
android:id="@+id/seekBar" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:layout_marginRight="20dp" | |
android:max="100" | |
android:progress="50"/> | |
<EditText | |
android:id="@+id/editText" | |
android:layout_width="80dp" | |
android:layout_height="wrap_content" | |
android:ems="10" | |
android:inputType="number" | |
android:gravity="center" | |
android:imeOptions="actionDone" | |
android:singleLine="true" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
seekBar = (SeekBar) findViewById(R.id.seekBar); | |
editText = (EditText) findViewById(R.id.editText); | |
//初始時,取德Seekbar的值,並將該值設為EditText的值 | |
int value = seekBar.getProgress(); | |
String stringValue = String.valueOf(value); | |
editText.setText(stringValue); | |
//設定EditText監聽事件,讓Seekbar可以取得EditText的值 | |
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() { | |
@Override | |
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { | |
//取得EditText的值並將該值設為Seekbar的值 | |
String progress = editText.getText().toString(); | |
int progressValue = Integer.parseInt(progress); | |
seekBar.setProgress(progressValue); | |
return false; | |
} | |
}); | |
//設定Seekbar監聽事件,讓EditText可以取得Seekbar的值 | |
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { | |
@Override | |
public void onStopTrackingTouch(SeekBar seekBar) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void onStartTrackingTouch(SeekBar seekBar) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { | |
// 當Seekbar的值變動的時候,取得Seekbar的值並將該值設為EditText的值 | |
int value = seekBar.getProgress(); | |
String stringValue = String.valueOf(value); | |
editText.setText(stringValue); | |
} | |
}); | |
} |
執行結果:
參考來源:Lutas
留言
張貼留言