ページ 11

androidでボタンを配置する

Posted: 2013年2月27日(水) 21:28
by shiro4ao
たびたびすみません。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>

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

Posted: 2013年2月27日(水) 23:50
by Dixq (管理人)
Ctrl + Shift + O を押して正しく必要なものをImportしてください。

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

Posted: 2013年2月28日(木) 00:09
by shiro4ao
ご回答ありがとうございます
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;
    }
    
}

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

Posted: 2013年2月28日(木) 10:43
by non
5行目の
import android.content.DialogInterface.OnClickListener;

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

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

Posted: 2013年2月28日(木) 13:16
by shiro4ao
ご回答ありがとうございます
import android.content.DialogInterface.OnClickListener;

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

ありがとうございます

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

Posted: 2013年2月28日(木) 20:17
by Dixq (管理人)
「正しく必要なものをImportしてください。」と書いたのはこのためです。
Importの選択肢が出ませんでしたか?
これからも似たようなことはあるかと思います。

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

Posted: 2013年2月28日(木) 21:09
by shiro4ao
赤いラインが消えただけだったので選択肢は出なかったように思います
インポートするべきものを覚えておこうと思います。

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

Posted: 2013年2月28日(木) 21:19
by Dixq (管理人)
添付ファイルのような選択肢が出たはずです。
その時上を選んだものと思います。

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

Posted: 2013年2月28日(木) 21:25
by shiro4ao
Ctrl + Shift + O を押したときにでました
ここからインポートすればいいのですね
以後気を付けます