英文:
1 event for multiple buttons
问题
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_general, container, false);
// Text to speech
tts = new Tts();
tts.init(getActivity(), "Reza");
Button btn = view.findViewById(R.id.hello); // Default button
switch (view.getId()) {
case R.id.hello:
btn = view.findViewById(R.id.hello);
break;
case R.id.observed:
btn = view.findViewById(R.id.observed);
break;
// Add more cases for other buttons if needed
}
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String id = getResources().getResourceEntryName(view.getId());
tts.speak(id);
}
});
return view;
}
错误是因为在您的代码中,您正在尝试在switch
语句中使用view.getId()
,但是这会导致资源ID为0xffffffff
,因此出现Resources$NotFoundException
异常。我已经对代码进行了修正,现在应该能够正常工作。
英文:
I have an event listener that listens only to 1 button (which is Button hello = view.findViewById(R.id.hello)
).
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_general, container, false);
Button hello = view.findViewById(R.id.hello);
// Text to speech
tts = new Tts();
tts.init(getActivity(), "Reza");
hello.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String id = getResources().getResourceEntryName(view.getId());
tts.speak(id);
}
});
return view;
}
Now I have multiple buttons and when you click on them, the same thing should happen. I do not want to copy and paste my code for all the other buttons.
This is what I have tried:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_general, container, false);
// Get the button
Button btn = view.findViewById(R.id.hello);
switch (getResources().getResourceEntryName(view.getId())) {
case "hello":
btn = view.findViewById(R.id.hello);
break;
case "observed":
btn = view.findViewById(R.id.observed);
break;
}
// Text to speech
tts = new Tts();
tts.init(getActivity(), "Reza");
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String id = getResources().getResourceEntryName(view.getId());
tts.speak(id);
}
});
return view;
}
But I get the error:
> android.content.res.Resources$NotFoundException: Unable to find
> resource ID #0xffffffff
There must be something wrong with my switch statement.
答案1
得分: 2
你可以在所有按钮的 XML 中使用 onClick 属性,如下所示:
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnHandler"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/fragment_container"
android:text="btn1"/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnHandler"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn1"
android:text="btn2"/>
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnHandler"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn2"
android:text="btn3"/>
在 btnHandler 中执行以下操作:
在 Kotlin 中:
fun btnHandler(view: View) {
when(view.id) {
R.id.btn1 -> Log.d(TAG, "btn1")
R.id.btn2 -> Log.d(TAG, "btn2")
R.id.btn3 -> Log.d(TAG, "btn3")
}
}
在 Java 中使用 switch-case(如果有错误,请修复,我目前不使用 Java,可能会犯错误):
public void btnHandler(View view) {
switch(view.getId()) {
case R.id.btn1:
Log.d(TAG, "btn1");
break;
case R.id.btn2:
Log.d(TAG, "btn2");
break;
case R.id.btn3:
Log.d(TAG, "btn3");
break;
}
}
英文:
You can use the property onClick in the xml of all buttons like so
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnHandler"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/fragment_container"
android:text="btn1"/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnHandler"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn1"
android:text="btn2"/>
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnHandler"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn2"
android:text="btn3"/>
And inside the btnHandler do the following:
In Kotlin:
fun btnHandler(view: View) {
when(view.id) {
R.id.btn1 -> Log.d(TAG, "btn1")
R.id.btn2 -> Log.d(TAG, "btn2")
R.id.btn3 -> Log.d(TAG, "btn3")
}
}
In java use switch-case (if there is an error you fix it I am not using java at the current time and might make mistake)
public void btnHandler(view: View) {
switch(view.id) {
case R.id.btn1:
Log.d(TAG, "btn1");
break;
case R.id.btn2:
Log.d(TAG, "btn2");
break;
case R.id.btn3:
Log.d(TAG, "btn3");
break;
}
}
答案2
得分: 0
你正在寻找可能不存在的 id
....
// 获取按钮
Button btn = view.findViewById(R.id.hello); /// 在这里,如果观察了该视图,它是否包含 R.id.hello?
switch (getResources().getResourceEntryName(view.getId())) {
英文:
You are looking for id that may not exists
....
// Get the button
Button btn = view.findViewById(R.id.hello); /// here if the view is observed, does it contain R.id.hello?
switch (getResources().getResourceEntryName(view.getId())) {
答案3
得分: 0
你的问题出在这个区域:
switch (getResources().getResourceEntryName(view.getId())) {
case "hello":
btn = view.findViewById(R.id.hello);
break;
case "observed":
btn = view.findViewById(R.id.observed);
break;
}
你正在对 fragment_general
视图上的按钮使用 switch 语句,试图将片段的 ID 赋给一个按钮。因此它将无法找到该 ID。我建议像这样实现:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_general, container, false);
// 获取按钮
Button btn = view.findViewById(R.id.hello);
Button observeBtn = view.findViewById(R.id.observed);
// 文字转语音
tts = new Tts();
tts.init(getActivity(), "Reza");
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
makeTTS("Hello");
}
});
observeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
makeTTS("Observe");
}
});
return view;
}
public void makeTTS(String aText){
tts.speak(aText);
}
英文:
Your issue lies within this area:
switch (getResources().getResourceEntryName(view.getId())) {
case "hello":
btn = view.findViewById(R.id.hello);
break;
case "observed":
btn = view.findViewById(R.id.observed);
break;
}
You are identifing the button and then using a switch statement on the fragment_general view thereby trying to assign the fragment ID to a button. So it will not find the ID. I would suggest implementing like so.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_general, container, false);
// Get the button
Button btn = view.findViewById(R.id.hello);
Button observeBtn = view.findViewById (R.id.observed);
// Text to speech
tts = new Tts();
tts.init(getActivity(), "Reza");
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
makeTTS("Hello")
}
});
observeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
makeTTS("Observe")
}
});
return view;
}
public void makeTTS(String aText){
tts.speak(aText);
}
答案4
得分: 0
这将很容易实现并简化您的代码,检查:
-
将
View.OnClickListener
实现到类中:public class Test extends Activity implements View.OnClickListener
-
在
onCreateView
方法中为每个按钮设置监听器:btnHello = view.findViewById(R.id.hello); btnObserved = view.findViewById(R.id.observed); btnHello.setOnClickListener(this); btnObserved.setOnClickListener(this);
-
然后,您必须重写设置
onClick
方法,如下所示:@Override public void onClick(View view) { switch (view.getId()){ case R.id.hello: case R.id.observed: tts.speak(view.getId()); break; } }
英文:
this will be easy to implement and simplify your code, check:
-
Implement
View.OnClickListener
to classpublic class Test extends Activity implements View.OnClickListener
-
On CreateView method set listeners for every button:
btnHello = view.findViewById(R.id.hello); btnObserved = view.findViewById(R.id.observed); btnHello.setOnClickListener(this); btnObserved.setOnClickListener(this);
-
Then, you have to set
onClick
method overrided, like this:@Override public void onClick(View view) { switch (view.getId()){ case R.id.hello: case R.id.observed: tts.speak(view.getId()); break; } }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论