Java 从XML Android Studio导入ID开关

Java 从XML Android Studio导入ID开关,java,android,android-view,Java,Android,Android View,我在Android Studio中遇到了问题,我不知道如何一般地导入交换机ID,因此我可以从所有交换机调用此函数。 我试着把开关的描述和它的编号放在一起,这样我就可以得到一个像“1”这样的数字 } 交换机的XML <Switch android:id="@+id/switch2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDe

我在Android Studio中遇到了问题,我不知道如何一般地导入交换机ID,因此我可以从所有交换机调用此函数。 我试着把开关的描述和它的编号放在一起,这样我就可以得到一个像“1”这样的数字

}

交换机的XML

<Switch
    android:id="@+id/switch2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="2"
    android:onClick="OnClick"
    android:text="switch2" />

<Switch
    android:id="@+id/switch1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="1"
    android:onClick="OnClick"
    android:text="switch1" />


对不起,我说的是英语。

要在代码中获取小部件的ID,只需获取
视图。getId()
它将返回一个用于标识视图的正整数, 要获取一般id,您可以调用

String mId = Integer.toString(view.getId());

当您单击
switch1
时,它将返回
switch1
id
,类似地,当您单击
switch2
时,它将返回
switch2
id

以获取代码中小部件的id。getId()
它将返回用于标识视图的正整数, 要获取一般id,您可以调用

String mId = Integer.toString(view.getId());

当您单击
switch1
时,它将返回
switch1
id
,同样,当您单击
switch2
时,它将返回
switch2
id

使用选中的已更改侦听器并在那里获取id。例如:

 <Switch
    android:id="@+id/switch1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="152dp"
    android:text="Switch1"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Switch
    android:id="@+id/switch2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="Switch2"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/switch1" />

//////////Java Code///////////

public class MainActivity extends AppCompatActivity {
Switch gSwitch,switch1, switch2;
boolean state;
int id;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    switch1 = findViewById(R.id.switch1);
    switch1 = findViewById(R.id.switch2);
    switch1.setOnCheckedChangeListener(checkedChangeListener);
    switch2.setOnCheckedChangeListener(checkedChangeListener);
}

CompoundButton.OnCheckedChangeListener checkedChangeListener = new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
       id = buttonView.getId();
        gSwitch = findViewById(id);// generic switch;
        state = isChecked;

        // call your method here for example:
       // BackgroundWorker backgroundWorker = new BackgroundWorker(this);
       // backgroundWorker.execute("switch", state, id);
    }
};

//////////Java代码///////////
公共类MainActivity扩展了AppCompatActivity{
开关G开关、开关1、开关2;
布尔状态;
int-id;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
开关1=findViewById(R.id.switch1);
开关1=findViewById(R.id.switch2);
开关1.setOnCheckedChangeListener(checkedChangeListener);
开关2.setOnCheckedChangeListener(checkedChangeListener);
}
CompoundButton.OnCheckedChangeListener checkedChangeListener=新建CompoundButton.OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){
id=buttonView.getId();
gSwitch=findviewbyd(id);//通用开关;
状态=已检查;
//在此处调用您的方法,例如:
//BackgroundWorker BackgroundWorker=新的BackgroundWorker(此);
//backgroundWorker.execute(“开关”,状态,id);
}
};

}

使用选中的已更改侦听器并在那里获取id。例如:

 <Switch
    android:id="@+id/switch1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="152dp"
    android:text="Switch1"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Switch
    android:id="@+id/switch2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="Switch2"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/switch1" />

//////////Java Code///////////

public class MainActivity extends AppCompatActivity {
Switch gSwitch,switch1, switch2;
boolean state;
int id;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    switch1 = findViewById(R.id.switch1);
    switch1 = findViewById(R.id.switch2);
    switch1.setOnCheckedChangeListener(checkedChangeListener);
    switch2.setOnCheckedChangeListener(checkedChangeListener);
}

CompoundButton.OnCheckedChangeListener checkedChangeListener = new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
       id = buttonView.getId();
        gSwitch = findViewById(id);// generic switch;
        state = isChecked;

        // call your method here for example:
       // BackgroundWorker backgroundWorker = new BackgroundWorker(this);
       // backgroundWorker.execute("switch", state, id);
    }
};

//////////Java代码///////////
公共类MainActivity扩展了AppCompatActivity{
开关G开关、开关1、开关2;
布尔状态;
int-id;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
开关1=findViewById(R.id.switch1);
开关1=findViewById(R.id.switch2);
开关1.setOnCheckedChangeListener(checkedChangeListener);
开关2.setOnCheckedChangeListener(checkedChangeListener);
}
CompoundButton.OnCheckedChangeListener checkedChangeListener=新建CompoundButton.OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){
id=buttonView.getId();
gSwitch=findviewbyd(id);//通用开关;
状态=已检查;
//在此处调用您的方法,例如:
//BackgroundWorker BackgroundWorker=新的BackgroundWorker(此);
//backgroundWorker.execute(“开关”,状态,id);
}
};
}使用视图绑定 对于使用视图绑定的每个模块,在module level build.gradle文件中将viewBinding build选项设置为true:

  buildFeatures {
        dataBinding = true
    }
并从kotlinx.android.synthetic中删除所有导入。 使用视图绑定 对于使用视图绑定的每个模块,在module level build.gradle文件中将viewBinding build选项设置为true:

  buildFeatures {
        dataBinding = true
    }
并从kotlinx.android.synthetic中删除所有导入。