2015-10-04 61 views

回答

1

我以前沒有這樣做,但是像這樣的東西應該可以工作: 讓您的類實現TextWatcher接口。

當您創建的TextView,添加textView.addTextChangedListener(this);

然後加:

public void afterTextChanged(Editable s) { 
    if(textView.getLineCount() >= 4) { 
     ToggleButton showMoreToggle = (ToggleButton) findViewById(R.id.showMoreToggle); 
     showMoreToggle.setVisibility(View.VISIBLE); 
    } 
} 
public void onTextChanged(CharSequence s,int start, int before, int count) {} 
public void beforeTextChanged(CharSequence s, int start, int count, int after) {} 

然後做一個切換按鈕在XML,並添加android:click="onClick"android:visibility="gone"。然後在你的活動代碼放:

public void onClick(View v) { 
    ToggleButton tb = (ToggleButton) v; 
    textView.setMaxLines(tb.isChecked() ? 10 : 4); 
} 
+0

感謝geokavel爲輸入時,setLines是剛剛設置的行數顯示在TextView的,它被丟棄行的TextView的休息 – antroid

+0

好,我加了現在有更多的東西,讓我知道它是怎麼回事。 – geokavel