ジャンプのアクティビティを完了するために、Buttonコントロールを使用します

あなたがButtonコントロールをクリックしたときに、いくつかの応答を作るボタンをクリックリスナー(ClickListener)を追加し、ここで私はクリックインターフェースジャンプ結果が完了した後に、それに追加するには:


まず、空のプロジェクトを作成し、それが自動的に元のアクティビティコードを生成します。

以下のようにジャンプの後のインタフェースである新しい活動インターフェース、新しいメソッドを作成し
ここに画像を挿入説明
、私はTextViewActivityという名前のこの新しい活動がします
ここに画像を挿入説明
、リソースファイルの描画可能に名dachuang.jpg写真を追加
ここに画像を挿入説明
TextViewActivityでXMLファイルを以下の記述を追加(ここに書かれたXMLに精通しているために、コードの私の作品があるが、実際には、当然のことながら、簡単に書くことができます)。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".TextViewActivity">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/text_view_head"
        android:textSize="30sp"
        android:gravity="center"/>
    <!--为了使得文字居中,需要设置gravity,也需要设置layoutwidth=match_parent-->
    <!--text内容最好使用引用,否则会认为是"Hardcoded"而报警告-->

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:src="@drawable/dachuang" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:drawableRight="@drawable/dachuang"
        android:text="@string/text_view_head"
        android:textSize="30sp" />
    <!--大图片不适合用TextView展示-->

    <View
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="#000000"/>
    <!--View控件不含有text属性-->

</LinearLayout>

ボタンのXML記述を作成されたXMLファイルの以下の記述を追加しますMainActivity


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/bt1"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:text="MyFirstButton"/>
      <!--text默认大写-->

</RelativeLayout>

そして、次のコードを追加しますMainActivity

package com.example.demo2;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button mBtn1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mBtn1 = findViewById(R.id.bt1);  //这里隐含View到Button的泛型转换
        mBtn1.setOnClickListener(new View.OnClickListener() {  //记得回来查下这里的文档
            @Override
            public void onClick(View v) {
                //跳转到TextView
                Intent intent = new Intent(MainActivity.this, TextViewActivity.class);
                startActivity(intent);
            }
        });
    }
}


これが完了します

ボタンの公式文書

ここに画像を挿入説明

公開された38元の記事 ウォンの賞賛9 ビュー6260

おすすめ

転載: blog.csdn.net/qq_42138454/article/details/104279185
おすすめ