Archive for the ‘Android’ Category

Android onClick 写法

Android | Posted by 老沙
7月 20 2017

package com.hello;

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

public class AActivity extends Activity {
/** Called when the activity is first created. */

EditText Ev1;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Ev1 = (EditText)findViewById(R.id.editText1);

//第一种方式
Button Btn1 = (Button)findViewById(R.id.button1);//获取按钮资源
Btn1.setOnClickListener(new Button.OnClickListener(){//创建监听
public void onClick(View v) {
String strTmp = “点击Button01″;
Ev1.setText(strTmp);
}

});

//第二种方式
Button Btn2 = (Button) findViewById(R.id.button2);//获取按钮资源
Btn2.setOnClickListener(listener);//设置监听

}

Button.OnClickListener listener = new Button.OnClickListener(){//创建监听对象
public void onClick(View v){
String strTmp=”点击Button02″;
Ev1.setText(strTmp);
}

};
//第三种方式(Android1.6版本及以后的版本中提供了)
public void Btn3OnClick(View view){
String strTmp=”点击Button03”;
Ev1.setText(strTmp);

}
}

android studio 新建项目报错 Failed to resolve: junit:junit:4.12

Android | Posted by 老沙
7月 20 2017

android studio 新建项目报错如下:

(27,17) Failed to resolve: junit:junit:4.12

Failed to resolve: javax.inject:javax.inject:1

Failed to resolve: javax.annotation:javax.annotation-api:1.2

Failed to resolve: com.google.code.findbugs:jsr305:2.0.1

Failed to resolve: org.hamcrest:hamcrest-library:1.3

Failed to resolve: org.hamcrest:hamcrest-integration:1.3

Failed to resolve: com.squareup:javawriter:2.1.1

解决办法:

注释gradle文件中的

compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
testCompile 'junit:junit:4.12'
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" (可删可不删的)