今日所學:
AS 按鈕的三種方式
(20條消息) Android studio使用小技巧之 快速生成onClick()點擊方法_kill_bugs的博客-CSDN博客
遇到的問題:
一開始在虛擬機上運行一直沒反應
后來在自己的手機上調試,有反應
后來新建了一個虛擬機,就可以了
還不知道問題出在哪里
明日計划:
實現App界面跳轉和數據的傳送
成果展示:
代碼:
MainActivity.java
import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; import static android.widget.Toast.LENGTH_LONG; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button bt3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.bt2).setOnClickListener(this); bt3=findViewById(R.id.bt3); bt3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(),"Button 3 clickd", LENGTH_LONG).show(); } }); } public void button_nn(View view) { Toast.makeText(getApplicationContext(),"Button 1 clickd",Toast.LENGTH_LONG).show(); } public void onClick(View v) { if(v.getId()==R.id.bt2) { Toast.makeText(getApplicationContext(),"Button 2 clicked",Toast.LENGTH_LONG).show(); } } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <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:orientation="vertical" tools:context=".MainActivity" > <Button android:onClick="button_nn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按鈕一" /> <Button android:id="@+id/bt2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按鈕二" /> <Button android:id="@+id/bt3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按鈕三" /> </LinearLayout>
最后祝大家 碼 到成功!