onclick事件不是在Android平台是在、事件、平台、onclick

由网友(天涯行客)分享简介:我已经写了code。使用按钮点击事件,Android中输入一些文字。这是不工作的。不给予任何错误或异常,我没有得到在那里都去错了。 公共无效的onCreate(捆绑savedInstanceState){    super.onCreate(savedInstanceState);    的setContentView...

我已经写了code。使用按钮点击事件,Android中输入一些文字。这是不工作的。不给予任何错误或异常,我没有得到在那里都去错了。

 公共无效的onCreate(捆绑savedInstanceState){    super.onCreate(savedInstanceState);    的setContentView(R.layout.main);    // select_btn =(按钮)findViewById(R.id.ok);    // home_btn =(按钮)findViewById(R.id.home);    等=(EditText上)findViewById(R.id.entry);    et.setText(,TextView.BufferType.EDITABLE);    B =新包();    的for(int i = 0; I< mybtn.length;我++){        字符串btnid =BTN+我;        INT渣油= getResources()。则getIdentifier(btnid,ID,                getPackageName());        mybtn [I] =(按钮)findViewById(渣油);        mybtn [I] .setOnClickListener(本);    }    //tts.speak("hello世界国际,TextToSpeech.QUEUE_FLUSH,NULL);    et.setText(你好人,TextView.BufferType.EDITABLE);}公共无效的onClick(视图v){    //而(tts.isSpeaking());    et.setText(你好Android的,TextView.BufferType.EDITABLE);    字符串s = NULL;    S =((按钮)V).getText()的toString()修剪()。    //tts.speak("hello谷歌,TextToSpeech.QUEUE_FLUSH,NULL);} 

这里是main.xml中:

 <的EditText    机器人:ID =@ + ID /条目    机器人:layout_width =FILL_PARENT    机器人:layout_height =WRAP_CONTENT    机器人:layout_marginTop =10dp    机器人:可聚焦=假    机器人:focusableInTouchMode =假    安卓的inputType =手机    机器人:最大长度=20/>< TableLayout    机器人:ID =@ + ID /表    机器人:layout_width =FILL_PARENT    机器人:layout_height =WRAP_CONTENT    机器人:layout_below =@ ID /条目    机器人:layout_marginTop =20dp    机器人:stretchColumns =*>    <&的TableRow GT;        <按钮            机器人:ID =@ + ID / btn0            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =WRAP_CONTENT            机器人:layout_weight =25            机器人:可点击=真            机器人:可聚焦=真            机器人:focusableInTouchMode =真            机器人:比重=中心            机器人:文字=@字符串/零            机器人:TEXTSIZE =10PT            机器人:知名度=看得见/>        <按钮            机器人:ID =@ + ID / BTN1            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =WRAP_CONTENT            机器人:layout_weight =25            机器人:可点击=真            机器人:可聚焦=真            机器人:focusableInTouchMode =真            机器人:比重=中心            机器人:文字=@字符串/一            机器人:TEXTSIZE =10PT            机器人:知名度=看得见/>        <按钮            机器人:ID =@ + ID / BTN2            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =WRAP_CONTENT            机器人:layout_weight =25            机器人:可点击=真            机器人:可聚焦=真            机器人:focusableInTouchMode =真            机器人:比重=中心            机器人:文字=@字符串/两个            机器人:TEXTSIZE =10PT            机器人:知名度=看得见/>        <按钮            机器人:ID =@ + ID / btn3            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =WRAP_CONTENT            机器人:layout_weight =25            机器人:可点击=真            机器人:可聚焦=真            机器人:focusableInTouchMode =真            机器人:比重=中心            机器人:文字=@字符串/三            机器人:TEXTSIZE =10PT            机器人:知名度=看得见/>    < /&的TableRow GT;    <查看        机器人:layout_height =2DIP        机器人:背景=#FF909090/>    <&的TableRow GT;        <按钮            机器人:ID =@ + ID / btn4            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =WRAP_CONTENT            机器人:layout_weight =25            机器人:可点击=真            机器人:可聚焦=真            机器人:focusableInTouchMode =真            机器人:比重=中心            机器人:文字=@字符串/四            机器人:TEXTSIZE =10PT            机器人:知名度=看得见/>        <按钮            机器人:ID =@ + ID / btn5            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =WRAP_CONTENT            机器人:layout_weight =25            机器人:可点击=真            机器人:可聚焦=真            机器人:focusableInTouchMode =真            机器人:比重=中心            机器人:文字=@字符串/五            机器人:TEXTSIZE =10PT            机器人:知名度=看得见/>        <按钮            机器人:ID =@ + ID / btn6            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =WRAP_CONTENT            机器人:layout_weight =25            机器人:可点击=真            机器人:可聚焦=真            机器人:focusableInTouchMode =真            机器人:比重=中心            机器人:文字=@字符串/六个一            机器人:TEXTSIZE =10PT            机器人:知名度=看得见/>        <按钮            机器人:ID =@ + ID / btn7            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =WRAP_CONTENT            机器人:layout_weight =25            机器人:可点击=真            机器人:可聚焦=真            机器人:focusableInTouchMode =真            机器人:比重=中心            机器人:文字=@字符串/七            机器人:TEXTSIZE =10PT            机器人:知名度=看得见/>    < /&的TableRow GT;    <查看        机器人:layout_height =2DIP        机器人:背景=#FF909090/>    <&的TableRow GT;        <按钮            机器人:ID =@ + ID / btn8            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =WRAP_CONTENT            机器人:layout_weight =25            机器人:可点击=真            机器人:可聚焦=真            机器人:focusableInTouchMode =真            机器人:比重=中心            机器人:文字=@字符串/八            机器人:TEXTSIZE =10PT            机器人:知名度=看得见/>        <按钮            机器人:ID =@ + ID / btn9            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =WRAP_CONTENT            机器人:layout_weight =25            机器人:可点击=真            机器人:可聚焦=真            机器人:focusableInTouchMode =真            机器人:比重=中心            机器人:文字=@字符串/九            机器人:TEXTSIZE =10PT            机器人:知名度=看得见/>        <按钮            机器人:ID =@ + ID / btn10            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =WRAP_CONTENT            机器人:layout_weight =25            机器人:可点击=真            机器人:可聚焦=真            机器人:focusableInTouchMode =真            机器人:比重=中心            机器人:文字=@字符串/删除            机器人:TEXTSIZE =10PT            机器人:知名度=看得见/>        <按钮            机器人:ID =@ + ID / btn11            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =WRAP_CONTENT            机器人:layout_weight =25            机器人:可点击=真            机器人:可聚焦=真            机器人:focusableInTouchMode =真            机器人:比重=中心            机器人:文字=@字符串/完成            机器人:TEXTSIZE =10PT            机器人:知名度=看得见/>    < /&的TableRow GT;< / TableLayout><查看    机器人:ID =@ + ID /厂景    机器人:layout_width =FILL_PARENT    机器人:layout_height =90dp    机器人:layout_below =@ ID /表>< /视图> 

解决方案 设置了布局文件android onClick back 但点击反应

在您的code你是 mybtn [I] .setOnClickListener(本),其中这个为您的活动提供参考。在 setOnClickListener()预计为参数的 View.OnClickListener 接口。这是一个实现了 View.OnClickListener 接口的对象。有两种方法,你可以这样做。

1)创建一个匿名的对象,例如

  mybtn [I] .setOnClickListener(新OnClickListener(){    @覆盖    公共无效的onClick(视图v){        ...您的code在这里     }  }); 

新的 OnClickListener()创建一个匿名对象,它是匿名的,因为我们不参照存储它。

2)在实现 View.OnClickListener()传递一个对象。如果你想在你的活动对象以传递给 setOnClickListener(),你就必须有它声明它实现 View.OnClickListener(),另外重写公共无效的onClick(视图v)方法例如

 公共类YourActvivity扩展活动实现View.OnClickListener {    的onCreate(){    }    ....等等    //实现View.OnClickListener接口    @覆盖    上单击(视图V)公共无效{         ...您的code在这里    } 

因为你有按钮的数组,并调用 setOnClickListener(本),其中这个为您的活动这一个方法将呼吁每个按键的,所以如果你想每个按钮做不同的事情,你就必须进行比较,这是在你的XML按钮ID的单击事件源视图id,否则所有按钮的行为在完全相同点击时的方式相同。

要找出哪个按钮被点击就可以查看这样

 开关(v.getId()){   案例R.id.btn0:       ...做的按钮0的东西;   打破;   案例R.id.btn1:      ...做的按钮1东西;   打破;   ...等等   } 

这传递到的onClick()回调的观点认为,点击事件源于并简单地在这里引用为 v的onClick (视图v)。第二种方法将为声明和任何对象(类)工作,贯彻 View.OnClickListener

I have written a code to enter some text using button click events in android. It is not working at all. Not giving any error or exception,i am not getting where it is all going wrong.

public void onCreate(Bundle savedInstanceState)
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //select_btn = (Button) findViewById(R.id.ok);
    //home_btn = (Button) findViewById(R.id.home);
    et = (EditText) findViewById(R.id.entry);
    et.setText("", TextView.BufferType.EDITABLE );
    b = new Bundle();

    for (int i = 0; i < mybtn.length; i++) {
        String btnid = "btn" + i;
        int resid = getResources().getIdentifier(btnid, "id",
                getPackageName());
        mybtn[i] = (Button) findViewById(resid);
        mybtn[i].setOnClickListener(this);          
    }       
    //tts.speak("hello wolrd",TextToSpeech.QUEUE_FLUSH,null);
    et.setText("hello man", TextView.BufferType.EDITABLE );     
}

public void onClick(View v)
{

    //while(tts.isSpeaking());
    et.setText("hello android", TextView.BufferType.EDITABLE );
    String s=null;
    s = ((Button)v).getText().toString().trim();
    //tts.speak("hello google",TextToSpeech.QUEUE_FLUSH,null);      
}

here is the main.xml :

<EditText
    android:id="@+id/entry"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:inputType="phone"
    android:maxLength="20" />

<TableLayout
    android:id="@+id/table"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/entry"
    android:layout_marginTop="20dp"
    android:stretchColumns="*" >

    <TableRow>

        <Button
            android:id="@+id/btn0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:clickable="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:gravity="center"
            android:text="@string/zero"
            android:textSize="10pt"
            android:visibility="visible" />

        <Button
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:clickable="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:gravity="center"
            android:text="@string/one"
            android:textSize="10pt"
            android:visibility="visible" />

        <Button
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:clickable="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:gravity="center"
            android:text="@string/two"
            android:textSize="10pt"
            android:visibility="visible" />

        <Button
            android:id="@+id/btn3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:clickable="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:gravity="center"
            android:text="@string/three"
            android:textSize="10pt"
            android:visibility="visible" />
    </TableRow>

    <View
        android:layout_height="2dip"
        android:background="#FF909090" />

    <TableRow>

        <Button
            android:id="@+id/btn4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:clickable="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:gravity="center"
            android:text="@string/four"
            android:textSize="10pt"
            android:visibility="visible" />

        <Button
            android:id="@+id/btn5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:clickable="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:gravity="center"
            android:text="@string/five"
            android:textSize="10pt"
            android:visibility="visible" />

        <Button
            android:id="@+id/btn6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:clickable="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:gravity="center"
            android:text="@string/six"
            android:textSize="10pt"
            android:visibility="visible" />

        <Button
            android:id="@+id/btn7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:clickable="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:gravity="center"
            android:text="@string/seven"
            android:textSize="10pt"
            android:visibility="visible" />
    </TableRow>

    <View
        android:layout_height="2dip"
        android:background="#FF909090" />

    <TableRow>

        <Button
            android:id="@+id/btn8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:clickable="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:gravity="center"
            android:text="@string/eight"
            android:textSize="10pt"
            android:visibility="visible" />

        <Button
            android:id="@+id/btn9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:clickable="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:gravity="center"
            android:text="@string/nine"
            android:textSize="10pt"
            android:visibility="visible" />

        <Button
            android:id="@+id/btn10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:clickable="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:gravity="center"
            android:text="@string/delete"
            android:textSize="10pt"
            android:visibility="visible" />

        <Button
            android:id="@+id/btn11"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:clickable="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:gravity="center"
            android:text="@string/finish"
            android:textSize="10pt"
            android:visibility="visible" />
    </TableRow>
</TableLayout>

<View
    android:id="@+id/view1"
    android:layout_width="fill_parent"
    android:layout_height="90dp"
    android:layout_below="@id/table" >
</View>

解决方案

In your code you are passing the activity into the setOnClickListener() method by mybtn[i].setOnClickListener(this) where this is a reference to your activity. The setOnClickListener() expects as a parameter a View.OnClickListener interface. That is any object that implements the View.OnClickListener Interface. There are two ways that you might do this.

1) Create an anonymous object e.g.

  mybtn[i].setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        ... your code here
     }
  });

the new OnClickListener() creates an anonymous object, it is anonymous because we do not store a reference to it.

2) Pass in an object that implements the View.OnClickListener(). If you wanted to pass in your activity object to setOnClickListener() you would have to have it declare that it implements View.OnClickListener() and additionally Override the public void onClick(View v) method e.g.

public class YourActvivity extends Activity implements View.OnClickListener {

    onCreate() {
    }
    ....etc

    //implement the View.OnClickListener interface
    @Override
    public void on Click(View v) {
         ...your code here
    }

because you have an array of buttons and calling setOnClickListener(this) where this is your activity this one method will be called for each of your buttons, so if you want each button to do something different you would have to compare the view id that was the source of the click event with the button id in your xml, otherwise all buttons will behave in exactly the same way when clicked.

to find out which button was clicked you can check like this

   switch(v.getId()) {
   case R.id.btn0:
       ...do button 0 stuff;
   break;
   case R.id.btn1:
      ...do button 1 stuff;
   break;
   ...etc
   }

the view that is passed into the onClick() callback is the view that the click event originated from and is referenced here simply as v onClick(View v). This second method will work for any object (Class) that declares and implements View.OnClickListener.

阅读全文

相关推荐

最新文章