跳到主要內容

[Android] 利用EditText修改SeekBar的值

要達到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的事件

例如:
<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>

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

留言

這個網誌中的熱門文章

[Android] 使用shape自訂形狀及陰影

shape可以定義下面四種類型的形狀:     rectangle- 矩形(直角矩形、圓角矩形),默認的形狀     oval- 橢圓形、圓形     line- 線形(實線、虛線)     ring- 環形、環形進度條 rectangle 執行結果: 如果填色要填入漸層色的話,需要將solid標籤改成gradient標籤,用法如下: 執行結果: oval 執行結果: oval和ring的漸層有三種類型(android:type):linear(線性)、radial(放射性)、sweep(掃描) 如果漸層效果為radial(放射性)的話,必須加上android:gradientRadius屬性(指定漸層的半徑)。 執行結果: line 線的高度是透過stroke的android:width屬性設置,而size的android:height是定義整個形狀區域的高度,所以 size的height必須大於stroke的width 。 執行結果: ring 屬於ring的屬性:     android:innerRadius 內環的半徑     android:thickness 環的厚度     android:useLevel 一般設為false,要不然環可能會無法顯示 執行結果: 將環設計成環狀進度條的樣式: 1,在shape標籤外再加上rotate標籤 rotate屬性:     android:fromDegrees:開始旋轉的角度位置     android:toDegrees:結束時轉到的角度位置     android:pivotX:旋轉起點的X軸座標位置,可以是數值、百分比、百分比p     android:pivotY:旋轉起點的Y軸座標位置,可以是數值、百分比、百分比p 2,在style.xml裡加上style樣式 3,在activity_main.xml裡加上progressBar標籤...

[隋堂筆記] Android 利用BaseAdapter來自訂ListView

BaseAdapter 可以讓使用者自己定義許多種 View ,像是 Spinner, ListView, GridView等,而且 可以重用View來節省資源 :使用setTag將View存起來,之後就可以利用getTag將數據取出來 Step1  在activity_main.xml中新增ListView 例如: Step2 在layout中新增list_content.xml,並排好想要的ListView item佈局 Step3 在MainActivity.java 新增一個類別BA並繼承BaseAdapter 透過繼承 BaseAdapter,我們可以重新覆寫4個方法:     public int getCount();     取得 ListView 列表 Item 的數量。通常數量就是從建構子傳入的陣列或是集合大小。     public Object getItem(int position);     取得 ListView 列表於 position 位置上的 Item。position 通常是資料在陣列或是集合上的位置。     public long getItemId(int position);     取得 ListView 列表於 position 位置上的 Item 的 ID,一般用 position 的值即可。     public View getView(int position, View view, ViewGroup viewGroup);     通常會設定與回傳View 作為顯示在這個 position 位置的 Item 的 View。 基本上只要處理兩個方法 getCount跟getView即可。 getCount回傳我所宣告的String陣列st1的長度。 在getView之前,要先取得LayoutInflater 再來要先宣告一個類別viewHolder,用來存放list_content.xml內所有元件的內容 接著判斷view是否為空,如果為空就要...

[iOS] Objective-C 自訂UITableViewCell樣式

Step1 在Storyboard中拉出TableView和按下TableView Cell後要切換的View,並設定對應的class檔 TableView連結至ListController類別,並設定Storyboard ID為ListController 要切換的View連結至ViewController類別,並設定Storyboard ID為viewController Step2 新增CustomCell.xib 新增File(File → New → File...),選擇View,並將檔案命名為CustomCell 開啟CustomCell.xib,把預設的View元件刪掉,然後從元件庫拖曳Table View Cell,並拉好想要的Cell內容排版 Step3 新增自訂Objective-C class給這個NIB檔 新增File(File → New → File...),選擇Cocoa Touch class 需要繼承UITableViewCell Step4 回到CustomCell.xib,將Class連結到CustomTableViewCell Step5 在CustomTableViewCell.h檔中,設定元件變數 Step6 在ListController.h中設定TableView的元件變數,且宣告一個陣列,負責儲存表格上要呈現的資料 Step7 在ListController.m  的viewDidLoad方法中設定TableView的兩個輸出口:dataSource、delegate,並初始化陣列資料 dataSource所連結到的View Controller用來提供表格上Cell的內容 delegate為指定哪一個View Controller需要處理使用者在表格上的操作 Step8 在ListController.m中,實作tableView:numberOfRowsInSection:方法 -通知Table View需要產生多少個Cell(儲存格)來顯示資料 Step9 在ListController.m中,實作tableView:cellForRowAtIndexPat...