第三單元控制元件
Button控制元件
知識點一:如何設定點選事件
(1)方法一:
首先對xml中想要設定點選的控制元件新增onclick屬性
即android onclick=“點選後要進行的函式名”
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按鈕1"
android:onClick="click"
/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按鈕2"
/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按鈕3"
/>
</LinearLayout>
然後在java檔案裡面寫點選函式傳入引數View表示實體也就是當前操作他的那個控制元件
建立私有物件變數Button a再在onCreate方法裡面用id獲取他->目的是:等會兒想在點選函式里寫他怎麼變
MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private Button a;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
a=findViewById(R.id.button1);
}
public void click(View v){
a.setText("按鈕一被點選");
}
}
也可以按哪個Button哪個Button變不一定非要Button1,按照下面這麼改
MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View v){
((Button)v).setText("按鈕一被點選");
}
}
這種方法的關鍵是要導包import android.view.View;
下面兩個案例
1.按按鈕3變按鈕1文字
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按鈕1"
/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按鈕2"
/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按鈕3"
android:onClick="click1"
/>
</LinearLayout>
MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button a;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
a=findViewById(R.id.button1);
}
public void click1(View v){
a.setText("我愛你");
}
}
案例2:點選按鈕3變最上面的文字(TextView裡)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
<TextView
android:id="@+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/love"
/>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按鈕1"
/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按鈕2"
/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按鈕3"
android:onClick="click1"
/>
</LinearLayout>
MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button a;
private TextView t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
a=findViewById(R.id.button1);
t=findViewById(R.id.txt1);
}
public void click1(View v){
a.setText("我愛你");
t.setText("qwe");
}
}
第二種方法匿名內部類
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
<TextView
android:id="@+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/love"
/>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按鈕1"
android:onClick="click"
/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按鈕2"
/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按鈕3"
/>
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
<TextView
android:id="@+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/love"
/>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按鈕1"
android:onClick="click"
/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按鈕2"
/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按鈕3"
/>
</LinearLayout>
對按鈕1實行匿名內部類來實現點選事件
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button a;
private TextView t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button a;
private TextView t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
a=findViewById(R.id.button1);
a.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {這個函式名onClick不能變
a.setText("I love you!");
}
});
}
}
a.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {這個函式名onClick不能變
a.setText("I love you!");
}
});
}
}
方法3 介面
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button a;
private Button b;
private TextView t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
a=findViewById(R.id.button1);
t=findViewById(R.id.txt1);
a.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
a.setText("I love you!");
}
});
b=findViewById(R.id.button2);
b.setOnClickListener(this);
}
public void click1(View v){
a.setText("我愛你");
t.setText("qwe");
}
@Override
public void onClick(View v) {
b.setText("WMY");
}
}
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button a;
private Button b;
private TextView t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
a=findViewById(R.id.button1);
t=findViewById(R.id.txt1);
a.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
a.setText("I love you!");
}
});
b=findViewById(R.id.button2);
b.setOnClickListener(this);
}
public void click1(View v){
a.setText("我愛你");
t.setText("qwe");
}
@Override
public void onClick(View v) {
b.setText("WMY");
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
<TextView
android:id="@+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/love"
/>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按鈕1"
android:onClick="click"
/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按鈕2"
android:onClick="click2"
/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按鈕3"
android:onClick="click1"
/>
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
<TextView
android:id="@+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/love"
/>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按鈕1"
android:onClick="click"
/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按鈕2"
android:onClick="click2"
/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按鈕3"
android:onClick="click1"
/>
</LinearLayout>
綠筆是關鍵,先寫
b=findViewById(R.id.button2);
b.setOnClickListener(this);
b.setOnClickListener(this);
然後this變紅報錯
然後點中this,Alt+Enter彈出這個選第二個實現介面
然後選第一個
最後在Onclick方法裡寫點選這個Button後你想幹的
public void onClick(View v) {
b.setText("WMY");
}
b.setText("WMY");
}
自適應(多語言)
第一步new resource file
第二步local,然後filename取strings
最後選地區ok
然後分別在每個語言的strings.xml下寫程式碼,注意那個沒有標語言的是預設strings.xml就是沒有選語言預設顯示
java檔案
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt=findViewById(R.id.txt);
}
public void click001(View v){
txt.setText(R.string.app_name);
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt=findViewById(R.id.txt);
}
public void click001(View v){
txt.setText(R.string.app_name);
//顯示strings.xml裡greetings裡對應那句話
Toast.makeText(MainActivity.this,R.string.greetings,Toast.LENGTH_LONG).show();;
}
}
Toast.makeText(MainActivity.this,R.string.greetings,Toast.LENGTH_LONG).show();;
}
}
語言在設定裡選
選完再開啟app就是對應該語言下應該顯示的(在他自己的strings.xml裡設定)