【问题标题】:为什么 SetText 在 OnClick() 函数中不起作用
【发布时间】:2022-01-23 09:01:21
【问题描述】:

我昨天才知道如何使用 Android Studio Code。当我需要在单击按钮时更改文本时遇到问题。

但是当我发短信时,它不起作用。

这是我的代码:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        View bubbleView = getLayoutInflater().inflate(R.layout.bubble_view, null);

        Button bubble = (Button) findViewById(R.id.start_bubble);
        bubble.setOnClickListener(this);// calling onClick() method

        Button predict = (Button) bubbleView.findViewById(R.id.predict);
        predict.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.start_bubble:
                startService(new Intent(getApplicationContext(), SimpleService.class));
            case R.id.predict:
                View bubbleView = getLayoutInflater().inflate(R.layout.bubble_view, null);
                TextView predict_text = (TextView) bubbleView.findViewById(R.id.predict_text);
                predict_text.setText("Hi"); // <--- It don't work :(
            default:
                break;
        }
    }
}

[编辑] [] 添加一些 .XML 文件 这是我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context="com.siddharthks.sampleapp.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Project KHKT"
        android:textColor="#AA000000"
        android:textSize="21sp" />

    <Button
        android:id="@+id/start_bubble"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Paste and Predict"
        android:textSize="18sp" />
</LinearLayout>

这是我的bubble_view.xml,它只是为了一个

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingStart="16dp"
            android:paddingTop="16dp"
            android:paddingEnd="16dp"
            android:text="Project KHKT"
            android:textColor="#AA000000"
            android:textSize="21sp" />

        <Button
            android:id="@+id/predict"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Paste and Predict"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/predict_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingStart="16dp"
            android:paddingTop="16dp"
            android:paddingEnd="16dp"
            android:textColor="#AA000000"
            android:textSize="21sp" />


    </LinearLayout>
</ScrollView>

你对我有什么建议吗?

【问题讨论】:

  • 布局不是Views。它们是Views 的蓝图,每次您使用inflate() 时,它都是一个新的、完全独立的实例,与您可能在其他地方使用的任何其他实例无关。您在MainActivity 中膨胀的R.layout.bubble_view 实例都不会添加到任何地方的屏幕层次结构中,因此无论您看到并与之交互的任何实例(可能是在SimpleService 中创建的实例)都不会受到代码在MainActivity.
  • 如果您也可以共享xml,那么找出实际问题将很容易。
  • 感谢您的解释,迈克。顺便说一句,你有什么建议来解决它吗?
  • 案例 R.id.predict: predict_text.setText("Hi"); //
  • 好的,我将在帖子中编辑并添加一些 xml 文件,bhaskarkh

标签: java android xml android-studio kotlin


【解决方案1】:

我不确定你为什么在活动类中膨胀“bubble_view.xml”布局。但是作为您的问题,有两种主要方法可以使按钮可点击。您的第一条评论中有一个很好的解释,由 Mike M 完成。一旦您膨胀布局,它将创建一个新实例。

第一答案,假设您想要活动中的所有内容。

    public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button bubble;
    private Button predict;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initUIViews() // Initialze UI Views
        initUIActions() // Initialize Ui Actions 
    }

    private void initUiViews() {    
        Button bubble = (Button) findViewById(R.id.start_bubble);
        Button predict = (Button) bubbleView.findViewById(R.id.predict);
    }

     private void initUIActions() {
        bubble.setOnClickListener(this);// calling onClick() method 
        predict.setOnClickListener(this);
     }    

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.start_bubble:
                startService(new Intent(getApplicationContext(), SimpleService.class)); 
                break;
            case R.id.predict:
                predict_text.setText("Hi");  
                break;
            default:
                break;
        }
    }
}

并重组您的 XML 布局如下。重构这些布局的方法很少,我会写最简单的方法,但请注意,这不是最佳方法。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context="com.siddharthks.sampleapp.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Project KHKT"
        android:textColor="#AA000000"
        android:textSize="21sp" />

    <Button
        android:id="@+id/start_bubble"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Paste and Predict"
        android:textSize="18sp" />

    <!-- include bubble layout file -->
    <include layout="@layout/bubble_view.xml" />

</LinearLayout>

除了包含标签,您还可以将里面的整个代码添加到 Activity 布局中。

第二个答案,假设您想要带有气泡视图的活动和服务。

如果您正在寻找气泡视图,则必须创建气泡服务。

检查这个答案:Bubble Example 官方文档:Android Bubble

【讨论】:

    【解决方案2】:
    public class MainActivity extends AppCompatActivity implements View.OnClickListener{
        TextView predict_text;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button start = (Button) findViewById(R.id.start_bubble);
    
            LinearLayout parent = findViewById(R.id.activity_main);  
            View childView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.bubble_view, parent, false);
            parent.addView(childView);
            Button predict = (Button) childView.findViewById(R.id.predict);
            predict_text = (TextView) childView.findViewById(R.id.predict_text);
    
            predict.setOnClickListener(this);
            start.setOnClickListener(this);
    
    
    
        }
    
        @Override
        public void onClick(View view) {
    
            switch (view.getId()){
                case R.id.predict:
                    predict_text.setText("Hi"); 
                    break;
            }
        }
    }
    

    【讨论】:

    • 不行kkkkk :(
    【解决方案3】:

    break 添加到switch 中的每个case 语句中。

    【讨论】:

    • 我试过了,但是不行:(
    【解决方案4】:

    试试这个,它会在你的父级中添加你的气泡视图,你可以从主布局中对该特定布局执行任何操作。

    public class MainActivity extends AppCompatActivity  {
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
    
    LinearLayout parent = findViewById(R.id.activity_main);        //parent layout.
            View childView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.bubble_view,parent,false);
                
            parent.addView(childView);
           
    
    
            Button predict = (Button) childView.findViewById(R.id.predict);
    
    
            TextView predict_text = (TextView) childView.findViewById(R.id.predict_text);
    
            
    
            predict.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    predict_text.setText("Hi"); // <--- It don't work :(
                }
            });
    }
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-14
    相关资源
    最近更新 更多