一个事件适用于多个按钮。

huangapple go评论78阅读模式
英文:

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

&lt;Button
    android:id=&quot;@+id/btn1&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:onClick=&quot;btnHandler&quot;
    app:layout_constraintEnd_toEndOf=&quot;parent&quot;
    app:layout_constraintStart_toStartOf=&quot;parent&quot;
    app:layout_constraintTop_toTopOf=&quot;@+id/fragment_container&quot;
    android:text=&quot;btn1&quot;/&gt;

&lt;Button
    android:id=&quot;@+id/btn2&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:onClick=&quot;btnHandler&quot;
    app:layout_constraintEnd_toEndOf=&quot;parent&quot;
    app:layout_constraintStart_toStartOf=&quot;parent&quot;
    app:layout_constraintTop_toBottomOf=&quot;@+id/btn1&quot;
    android:text=&quot;btn2&quot;/&gt;

&lt;Button
    android:id=&quot;@+id/btn3&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:onClick=&quot;btnHandler&quot;
    app:layout_constraintEnd_toEndOf=&quot;parent&quot;
    app:layout_constraintStart_toStartOf=&quot;parent&quot;
    app:layout_constraintTop_toBottomOf=&quot;@+id/btn2&quot;
    android:text=&quot;btn3&quot;/&gt;

And inside the btnHandler do the following:

In Kotlin:

fun btnHandler(view: View) {
    when(view.id) {
        R.id.btn1 -&gt; Log.d(TAG, &quot;btn1&quot;)
        R.id.btn2 -&gt; Log.d(TAG, &quot;btn2&quot;)
        R.id.btn3 -&gt; Log.d(TAG, &quot;btn3&quot;)
    }
}

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, &quot;btn1&quot;);
            break;
        case R.id.btn2: 
            Log.d(TAG, &quot;btn2&quot;);
            break;
        case R.id.btn3: 
            Log.d(TAG, &quot;btn3&quot;);
            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 &quot;hello&quot;:
                btn = view.findViewById(R.id.hello);
                break;
            case &quot;observed&quot;:
                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(), &quot;Reza&quot;);
    
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              makeTTS(&quot;Hello&quot;)
            }
        });
        observeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              makeTTS(&quot;Observe&quot;)
            }
        });

        return view;
    }

  public void makeTTS(String aText){
  
                tts.speak(aText);
  }

答案4

得分: 0

这将很容易实现并简化您的代码,检查:

  1. View.OnClickListener 实现到类中:

    public class Test extends Activity implements View.OnClickListener
    
  2. onCreateView 方法中为每个按钮设置监听器:

    btnHello = view.findViewById(R.id.hello);
    btnObserved = view.findViewById(R.id.observed);
    btnHello.setOnClickListener(this);
    btnObserved.setOnClickListener(this);
    
  3. 然后,您必须重写设置 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:

  1. Implement View.OnClickListener to class

    public class Test extends Activity implements View.OnClickListener

  2. 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);
    
  3. 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;
        }
    }
    

huangapple
  • 本文由 发表于 2020年9月15日 04:57:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63891738.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定