androidでボタンを配置する

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
アバター
shiro4ao
記事: 224
登録日時: 13年前
住所: 広島

androidでボタンを配置する

#1

投稿記事 by shiro4ao » 11年前

たびたびすみません。m(_ _)m

Androidの館2.1 ボタンのタップイベントを取得するを参考にして
ボタンをつくるコードを書いているのですが、
public class ButtonSample01 extends Activity implements OnClickListener{
の OnClickListenerと
btn.setOnClickListener(this);のsetOnClickListener部分に赤いラインが引かれエラーになってしまいました。
いったいなにが足りないのでしょうか・・・。

コード:


package com.example.buttonsample01;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class ButtonSample01 extends Activity  implements OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button btn = (Button)findViewById(R.id.sampleButton);
        btn.setOnClickListener(this);
    }

    public void onClick(View v) {
        Toast.makeText(this, "ボタンが押されました", Toast.LENGTH_LONG).show();
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.button_sample01, menu);
        return true;
    }
    
}


コード:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".ButtonSample01" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/sampleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="34dp"
        android:layout_marginTop="102dp"
        android:text="Button" />

</RelativeLayout>

アバター
Dixq (管理人)
管理人
記事: 1661
登録日時: 13年前
住所: 北海道札幌市
連絡を取る:

Re: androidでボタンを配置する

#2

投稿記事 by Dixq (管理人) » 11年前

Ctrl + Shift + O を押して正しく必要なものをImportしてください。

アバター
shiro4ao
記事: 224
登録日時: 13年前
住所: 広島

Re: androidでボタンを配置する

#3

投稿記事 by shiro4ao » 11年前

ご回答ありがとうございます
Ctrl + Shift + O を押したのですが
public class ButtonSample01 extends Activity implements OnClickListenerのButtonSample01と
btn.setOnClickListener(this);のsetOnClickListenerの部分に赤いラインが引かれエラーになってしまいました。

すみません。

コード:


package com.example.buttonsample01;
 
import android.app.Activity;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
 
public class ButtonSample01 extends Activity  implements OnClickListener{
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button btn = (Button)findViewById(R.id.sampleButton);
        btn.setOnClickListener(this);
    }
 
    public void onClick(View v) {
        Toast.makeText(this, "ボタンが押されました", Toast.LENGTH_LONG).show();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.button_sample01, menu);
        return true;
    }
    
}

non
記事: 1097
登録日時: 13年前

Re: androidでボタンを配置する

#4

投稿記事 by non » 11年前

5行目の
import android.content.DialogInterface.OnClickListener;

import android.view.View.OnClickListener;
に変えてみてもおなじですか?
non

アバター
shiro4ao
記事: 224
登録日時: 13年前
住所: 広島

Re: androidでボタンを配置する

#5

投稿記事 by shiro4ao » 11年前

ご回答ありがとうございます
import android.content.DialogInterface.OnClickListener;

import android.view.View.OnClickListener;
に変えたらエラーが消え実行できました

ありがとうございます

アバター
Dixq (管理人)
管理人
記事: 1661
登録日時: 13年前
住所: 北海道札幌市
連絡を取る:

Re: androidでボタンを配置する

#6

投稿記事 by Dixq (管理人) » 11年前

「正しく必要なものをImportしてください。」と書いたのはこのためです。
Importの選択肢が出ませんでしたか?
これからも似たようなことはあるかと思います。

アバター
shiro4ao
記事: 224
登録日時: 13年前
住所: 広島

Re: androidでボタンを配置する

#7

投稿記事 by shiro4ao » 11年前

赤いラインが消えただけだったので選択肢は出なかったように思います
インポートするべきものを覚えておこうと思います。

アバター
Dixq (管理人)
管理人
記事: 1661
登録日時: 13年前
住所: 北海道札幌市
連絡を取る:

Re: androidでボタンを配置する

#8

投稿記事 by Dixq (管理人) » 11年前

添付ファイルのような選択肢が出たはずです。
その時上を選んだものと思います。
添付ファイル
scr.png
scr.png (18.02 KiB) 閲覧数: 3125 回

アバター
shiro4ao
記事: 224
登録日時: 13年前
住所: 広島

Re: androidでボタンを配置する

#9

投稿記事 by shiro4ao » 11年前

Ctrl + Shift + O を押したときにでました
ここからインポートすればいいのですね
以後気を付けます

閉鎖

“C言語何でも質問掲示板” へ戻る