爱玺玺

爱玺玺的生活日记本。wx:lb87626

android使用OnClickListerner监听事件,点击按钮后程序竟然会被关闭。

package cn.lb.myapp2;


import android.support.v7.app.ActionBarActivity;

import android.content.res.Resources;

import android.graphics.Color;

import android.graphics.drawable.Drawable;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.*;



public class MainActivity extends ActionBarActivity {

private TextView textView;

private Button button;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        Resources resources=this.getResources();

        Drawable drawable=resources.getDrawable(R.drawable.bkcolor);

        this.getWindow().setBackgroundDrawable(drawable);

        setContentView(R.layout.activity_main);

        TextView textView=(TextView) this.findViewById(R.id.textView2);

        textView.setBackgroundColor(Color.parseColor("#ff0000"));

        button=(Button) this.findViewById(R.id.button);

        MyClickEvent myClickEvent=new MyClickEvent();

        button.setOnClickListener(myClickEvent);

    }



    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.main, menu);

        return true;

    }


    @Override

    public boolean onOptionsItemSelected(MenuItem item) {

        // Handle action bar item clicks here. The action bar will

        // automatically handle clicks on the Home/Up button, so long

        // as you specify a parent activity in AndroidManifest.xml.

        int id = item.getItemId();

        if (id == R.id.action_settings) {

            return true;

        }

        return super.onOptionsItemSelected(item);

    }

    

    class MyClickEvent implements OnClickListener{


@Override

public void onClick(View button) {

// TODO Auto-generated method stub

System.out.println("点击事件被触发");

textView.setBackgroundColor(Color.parseColor("#0000ff"));

}

}

}




<LinearLayout 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:orientation="vertical"

    tools:context="cn.lb.myapp2.MainActivity" >


    <TextView

        android:id="@+id/textView2"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:background="#f00"

        android:text="@string/hello_world" />


    <Button 

        android:id="@+id/button"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="button"

        />"



</LinearLayout>



用android Studio不会出现这个问题。

发表评论:

Powered By Z-BlogPHP 1.4 Deeplue Build 150101

Copyright Your WebSite.Some Rights Reserved.

蜀ICP备11021721号-5