【问题标题】:How to know which button user clicked and its text and id in Android Studio?如何知道用户点击了哪个按钮及其在 Android Studio 中的文本和 ID?
【发布时间】:2021-07-20 14:11:17
【问题描述】:

在整个 android 应用程序中,我想捕捉按钮点击、单选按钮点击、链接点击等......基本上是整个 android 应用程序中的用户交互。有没有常用的方法来检测用户点击了哪个元素及其值?

【问题讨论】:

  • 在这里尝试使用片段内部活动,您可以获得按钮的值,或者您想要从多个片段到主要活动中的一个位置....另一种方法可以进入您可以访问的应用程序类使用应用程序实例在应用程序内的任何位置。

标签: android android-studio mobile-application buttonclick


【解决方案1】:

尝试在按钮上使用 onCickListener()。

【讨论】:

    【解决方案2】:

    在 Kotlin 中:

    1. 使用android:id="@+id/nameOfButton" 将ID 添加到.xml 文件中的按钮。每个按钮都需要一个唯一的名称。
    2. 在 .kt 文件中,使用带有 id 的setOnClickListener 来设置用户单击按钮时的操作。
    3. 如果有多个按钮,只需执行第 1 步和第 2 步即可。

    示例:
    第一步

       <Button
        android:id="@+id/buttonSave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save" />
    

    第二步

    buttonSave.setOnclickListenter {
        //TODO: your code goes here
    }
    

    【讨论】:

    • 感谢贾斯汀的帮助。但我需要它通用的方式。我需要跟踪应用程序中的所有按钮或单击的元素。
    【解决方案3】:

    非常简单,你只需要从 onclick 方法中获取按钮的文本和 id 即可。这是它的 java 和 xml 代码:
    XML:

    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/button"
      android:onclick="getid"
      android:text="Save" />
    

    JAVA:

    public void getid(View v){
      int id = v.getId();
      String text = v.getText();
    }
    

    【讨论】:

    • 感谢您的回答,以上代码仅适用于“getid”,但我需要以动态方式使用它。但需要方式,在应用程序中,无论用户单击什么按钮,我都需要详细信息。它应该对应用程序中的所有按钮都是通用的。
    • 然后你可以将button.setOnClickListener(getid());设置为所有需要的按钮
    • 所以它会捕获应用程序中的所有按钮吗?
    • 你需要在每一个中单独实现它们。就我而言,没有办法一次对所有按钮执行此操作。
    • 否则有什么方法可以捕捉我点击的元素,比如按钮或图标或下拉菜单或复选框?
    【解决方案4】:

    @Muthukumar Lenin 在这里问的是 xml 和 java 中的列表视图

    XML:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:tools="http://schemas.android.com/tools"
       android:id="@+id/rl"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:padding="10dp"
       tools:context=".MainActivity">
       <TextView
          android:id="@+id/textview"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:textStyle="italic"
          android:textColor="#5f65ff"
          android:padding="5dp"
          android:text="Choose is the best football player?"/>
       <ListView
          android:id="@+id/listview"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@id/textview" />
    </RelativeLayout>
    
    

    JAVA:

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.TextView;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    public class MainActivity extends AppCompatActivity {
       @Override
       protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          ListView listView = (ListView) findViewById(R.id.listview);
          final TextView textView = (TextView) findViewById(R.id.textview);
          String[] players = new String[] {"CR7", "Messi", "Hazard", "Neymar"};
          List<String> Players_list = new ArrayList<String>(Arrays.asList(players));
          ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Players_list);
          listView.setAdapter(arrayAdapter);
          listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
             @Override
             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String selectedItem = (String) parent.getItemAtPosition(position);
                textView.setText("The best football player is : " + selectedItem);
             }
          });
       }
    }
    

    其中一些代码来自tutorialspoint,一些是我编辑的。

    【讨论】:

      猜你喜欢
      • 2014-08-31
      • 2017-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多