亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

單擊按鈕后如何使文本顯示

單擊按鈕后如何使文本顯示

鴻蒙傳說 2021-10-17 16:02:44
我正在嘗試制作一個簡單的應用程序,當您單擊按鈕文本時會出現該應用程序。但是我遇到了一個問題,當我點擊按鈕時什么也沒有發生。public class MainActivity extends AppCompatActivity {    private TextView textView;    private Button button;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        textView = (TextView) findViewById(R.id.textView);        textView.setMovementMethod(new ScrollingMovementMethod());        button = (Button) findViewById(R.id.button);        View.OnClickListener ourOnClickListener = new View.OnClickListener() {            @Override            public void onClick(View v) {                textView.setText("It real sent your at. Amounted all shy set why followed declared. Repeated of endeavor mr position kindness offering ignorant so up. Simplicity are melancholy preference considered saw companions. Disposal on outweigh do speedily in on. Him ham although thoughts entirely drawings. Acceptance unreserved.");            }        };    }}XML  <Button        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginBottom="84dp"        android:layout_marginEnd="8dp"        android:layout_marginStart="8dp"        android:text="Button"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent" />    <TextView        android:id="@+id/textView"        android:layout_width="374dp"        android:layout_height="331dp"        android:layout_marginEnd="8dp"        android:layout_marginStart="8dp"        android:layout_marginTop="16dp"        android:textSize="8sp"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent" />
查看完整描述

3 回答

?
交互式愛情

TA貢獻1712條經驗 獲得超3個贊

您正在啟動onClickListener,但您沒有將其附加到任何對象。嘗試將 附加onClickListener到您的按鈕,如下所示:

button.setOnClickListener(ourOnClickListener);


查看完整回答
反對 回復 2021-10-17
?
天涯盡頭無女友

TA貢獻1831條經驗 獲得超9個贊

單擊時什么也沒有發生,因為您還沒有onClickListener在按鈕上設置,您只定義了onClick. 在您的onCreate: 中 添加這一行button.setOnClickListener(ourOnClickListener)。


查看完整回答
反對 回復 2021-10-17
?
一只萌萌小番薯

TA貢獻1795條經驗 獲得超7個贊

解決方案:


取而代之的是:


View.OnClickListener ourOnClickListener = new View.OnClickListener() {

    @Override

    public void onClick(View v) {

        textView.setText("It real sent your at. Amounted all shy set why followed declared. Repeated of endeavor mr position kindness offering ignorant so up. Simplicity are melancholy preference considered saw companions. Disposal on outweigh do speedily in on. Him ham although thoughts entirely drawings. Acceptance unreserved.");

    }

};

像這樣寫:


button.setOnClickListener(new View.OnClickListener) {

    @Override

    public void onClick(View v) {

        textView.setText("It real sent your at. Amounted all shy set why followed declared. Repeated of endeavor mr position kindness offering ignorant so up. Simplicity are melancholy preference considered saw companions. Disposal on outweigh do speedily in on. Him ham although thoughts entirely drawings. Acceptance unreserved.");


    }

};

希望能幫助到你。


查看完整回答
反對 回復 2021-10-17
  • 3 回答
  • 0 關注
  • 371 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號