【问题标题】:Add button and functionality yo appWidget添加按钮和功能哟 appWidget
【发布时间】:2021-02-18 16:34:26
【问题描述】:

好的,我正在这里学习。我刚刚学会了如何创建一个基本的 appWidget 并且到目前为止它可以工作。现在尽我所能,我不明白如何将图像按钮添加到我的 appWidget。我的意思是它已物理添加到名为 simple_app_widget.xml 的 xml 文件中。无法弄清楚的是如何将 onClick 事件添加到 SimpleAppWidget.java 文件?我已经尝试过像普通的单击事件一样,但 AIDE 告诉我它无法识别正在输入的信息。

请不要只为我做我的工作。我想学习,但我缺乏搜索技能,而且我发现的内容与我的技能水平无关或不相关。

【问题讨论】:

    标签: android coding-style android-appwidget aide-ide


    【解决方案1】:

    正如您所说...添加图像按钮首先必须在您的可绘制文件夹中添加图像(在您的项目树中搜索它),然后转到要使用它的布局并像这样编写代码(注意: 在, android:background =" " 必须输入你在drawable文件夹中复制的图片的同名):

    <ImageButton
        android:id="@+id/arrow"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginBottom="4dp"
        android:background="@drawable/arrow"
        android:contentDescription="@string/test"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.902"
        app:layout_constraintStart_toStartOf="parent" />
    

    之后,转到您的 Activity.class 并输入您的代码:

    public class MainActivity extends AppCompatActivity {
    
    ImageButton test;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        test = findViewById(R.id.test);
    
        test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Here should put what you want to show or where you want to go after click the button.
                
                //For example if you want to go to the next activity use Intents like this:
                
                Intent intent = new Intent(this, MainActivity1.class);
                startActivity(intent);
                finish();
            }
        });
    }
    

    }

    【讨论】:

    • 我可以使用android:src="@drawable/arrow"吗?
    • 是的,你可以使用它
    • 对我仍然不明白的部分是为什么当我输入你所放下的基本内容时,AIDE 是否告诉我它不能使用 findViewById?它也不能识别我在 public void 声明之后输入的任何内容。
    • 请编辑您的问题并添加您的代码,这样会更容易为您提供帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    相关资源
    最近更新 更多