首页 \ 问答 \ 怎么配置eclipse+java开发环境

怎么配置eclipse+java开发环境

更新时间:2021-12-30 21:12

最满意答案

1、Context说明       
       Context是一个用于访问全局信息的接口,如应用程序的资源(如图片,字符串等),一些常用的组件继承自Context,如Activity和Service等等。
       如利用Java代码创建一个textView,textView的第一种setText()方法直接传入一个字符串,第二种setText()方法传入一个整形的id(位于values\\strings.xml下面的hello_world,即:Hello world!),最后将setContextView改为setContextView(textView)。

[java] view plain copy 
public class MainActivity extends ActionBarActivity {  
    private TextView textView;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        //setContentView(R.layout.activity_main);  

        textView=new TextView(MainActivity.this);  
        //textView.setText("Hello GeoStorm");  

        textView.setText(R.string.hello_world);  

        setContentView(textView);  
    }  
}  
       以上代码中,双击第一个setText()方法,按F4,即可看到该构造函数,入口参数为字符串。

[java] view plain copy 
public final void setText(CharSequence text) {  
        setText(text, mBufferType);  
    }  
       双击第一个setText()方法,按F4,即可看到该构造函数,入口参数为resid,其内部的setText()即为第一个setText()方法,getContext()即为我们创建TextView时传入的MainActivity.this!!!

[java] view plain copy 
public final void setText(int resid) {  
    setText(getContext().getResources().getText(resid));  
}  
       在看了构造函数之后,我们也可以按照构造函数里的,进行下面的原始写法:

[java] view plain copy
//textView.setText(R.string.hello_world);  
textView.setText(getApplicationContext().getResources().getText(R.string.hello_world));  
       同样,我们也可以访问图片资源:

[java] view plain copy
public class MainActivity extends ActionBarActivity {  
    private TextView textView;  
    private ImageView imageView;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        //setContentView(R.layout.activity_main);  

        //textView=new TextView(MainActivity.this);  
        //textView.setText("Hello GeoStorm");  
        //textView.setText(R.string.hello_world);  
        //textView.setText(getApplicationContext().getResources().getText(R.string.hello_world));  

        imageView=new ImageView(this);//可以直接使用this  
        imageView.setImageResource(R.mipmap.ic_launcher);  
        setContentView(imageView);  
    }  
}  
2、Application的应用及说明
       现在我们用Context来实现组件之间信息的共享。创建一个App.java类用来传递数据,继承自Application,增加一个字符串变量,并添加读写构造函数,如下:

[java] view plain copy
package com.example.lhb.context;  

import android.app.Application;  

public class App extends Application {  
    public String S="DefaultString";  

    public void setS(String s) {  
        S = s;  
    }  

    public String getS() {  
        return S;  
    }  
}  
       接下来进行AndroidManifest配置:在Application中加入android:name=".App",并添加另一个Activity,名为Main2。在配置文件中加入跟MainActivity一样的配置,其中 说明在手机桌面上出现两个应用程序。

[java] view plain copy
  
  

      
          
              
                  

                  
              
          
          
              
                  

                  
              
          
      

  
       Main1中和Main2加入如下同样的代码:

[java] view plain copy
package com.example.lhb.context;  

import android.support.v7.app.ActionBarActivity;  
import android.os.Bundle;  
import android.view.Menu;  
import android.view.MenuItem;  
import android.view.View;  
import android.widget.EditText;  
import android.widget.ImageView;  
import android.widget.TextView;  

public class MainActivity extends ActionBarActivity {  
    private TextView textView;  
    private EditText editText;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  

        textView= (TextView) findViewById(R.id.textView);  
        editText= (EditText) findViewById(R.id.editText);  

        //读取默认的字符串  
        textView.setText("共享的数据是:"+((App)getApplicationContext()).getS());  

        findViewById(R.id.btnSave).setOnClickListener(new View.OnClickListener() {  
            @Override  
            public void onClick(View v) {  
                ((App)getApplicationContext()).setS(editText.getText().toString());  
                textView.setText(String.format("共享的数据是:%s",editText.getText().toString()));  
            }  
        });  
    }  
}  
效果如下:

3、Application生命周期
       重写构造函数并执行,结果发现App.java的onCreate()函数在任何一个Activity的onCreate()函数之前执行,这对于我们做初始化操作非常有用。

[java] view plain copy
package com.example.lhb.context;  

import android.app.Application;  

public class App extends Application {  
    public String S="DefaultString";  

    public void setS(String s) {  
        S = s;  
    }  

    public String getS() {  
        return S;  
    }  

    @Override  
    //创建时执行  
    public void onCreate() {  
        super.onCreate();  
        System.out.println("App OnCreate");  
    }  

    @Override  
    //结束时执行,一般不执行,只在模拟环境中执行  
    public void onTerminate() {  
        super.onTerminate();  
        System.out.println("App on Terminate");  
    }  

    @Override  
    //低内存时执行  
    public void onLowMemory() {  
        super.onLowMemory();  
    }  

    @Override  
    //清理内存时执行  
    public void onTrimMemory(int level) {  
        super.onTrimMemory(level);  
    }  
}

其他回答

android studio作为面市不久的安卓开发工具,越来越受到大家的喜爱,这里我将介绍如何在android studio中创建一个app项目,并在以后经验中介绍其他有关android studio的操作和编程方法。

工具/原料
android studio
方法/步骤
1
打开软件,在菜单中选择file-》new project打开创建向导。

2
配置项目,确定各个名称和存放项目存放路径;
application name:项目名称
company domain:公司域名
package name:app打包名称
project location:存放路径
最好将名称中的examples去掉。

3
接下来设定兼容的安卓的最小版本,这依情况而定,我这里设定为安卓2.3版本。

4
如果不确定各个版本的区别,可以点击“help me choose”,在打开的窗口中介绍了各个安卓版本的功能。
步骤阅读
5
确定后最小兼容版本后,选择项目的活动类型,这里有很多现成的模板可以使用,对于初学者选择默认的blank activity即可。

6
接下来设定活动名称,界面布局的名称,以及界面标题,最后点击finish完成项目的创建。

7
创建完后,在项目名-》app-》src-》main-》res-》layout下双击xml文件就是打开活动界面设计窗口了。

end
注意事项
注意根据情况合理选择安卓的最小兼容版本
 android studio教程 (共6篇)

相关问答

更多
  • 1、Context说明 Context是一个用于访问全局信息的接口,如应用程序的资源(如图片,字符串等),一些常用的组件继承自Context,如Activity和Service等等。 如利用Java代码创建一个textView,textView的第一种setText()方法直接传入一个字符串,第二种setText()方法传入一个整形的id(位于values\\strings.xml下面的hello_world,即:Hello world!),最后将setContextView改为setContextView ...
  • Android studio作为面市不久的安卓开发工具,越来越受到大家的喜爱,这里我将介绍如何在Android studio中创建一个APP项目,并在以后经验中介绍其他有关Android studio的操作和编程方法。 工具/原料 Android studio 方法/步骤 1 打开软件,在菜单中选择file-》new project打开创建向导。 2 配置项目,确定各个名称和存放项目存放路径; Application name:项目名称 Company Domain:公司域名 Package name:ap ...
  • Android Studio导入eclipse项目编译出错。 信息如下: AAPT: libpng error: Not a PNG file Execution failed for task ':app:mergeDebugResources'. > Some file crunching failed, see logs for details 如何学好英语: 1、牢记词汇。 2、掌握语法。 3、磨耳朵。 4、背诵范文。 5、勇敢说出来。
  • android studio的使用方式,其实很多和eclipse相似,你可以遇到问题,就百度一下,很多都有教程,具体的可以提问一下
  • C语言的异或操作是位操作的一种,其运算符号为^。 要打出这个符号,需要在英文输入法下,输入shift + 6 即 1 按下shift, 左右均可; 2 按6, 即按下,抬起; 3 抬起shift。 异或操作是按位操作,当两个操作数同一位上值相同时(同为1或同为0),结果对应位上的值为0;否则值为1。
  • 也许是你的电脑没有安装安卓驱动,android_winusb.inf
  • 不,使用“Android SDK文档”不会发生自动完成。 离线工作时可供参考。 例如,如果您编写一个没有文档的简单类,那么您也将自动完成。 因此,文档不用于自动完成。 另外,请查看此答案,以获取有关安全地从Android SDK中删除不需要的内容的提示。 No, the auto-complete does not happen using the "Documentation for Android SDK". It is there for reference when working offline. ...
  • 尝试将此添加到build.gradle文件 android{ aaptOptions { cruncherEnabled = false } } 注意:禁用它并不是一个好主意,因为你最终会增加apk大小(在大多数情况下),因为你通过这样做禁用图像大小优化虽然我仍然建议在某些图像编辑器中打开坏图像使用.png格式save as try adding this to build.gradle file android{ aaptOptions { ...
  • 您似乎遇到Android Studio中ConstraintLayout编辑器的已知错误。 看到: https://issuetracker.google.com/issues/37325425 和 https://issuetracker.google.com/issues/37137698 您应该能够通过将“中心水平”或“中心垂直”命令应用于当前在编辑器中选择的视图集合来创建链。 You appear to be running into a known bug with the ConstraintL ...
  • 转到设备管理器然后它可能会显示您的手机名称或某个未知设备点击它; 转到更新驱动程序,然后单击“在计算机上浏览驱动程序软件”,然后单击“让我从计算机上的设备驱动程序列表中选择”,然后将打开一个设备列表; 在那里搜索“android”并点击安装,这将解决问题 go to device manager then it may be showing the name of your phone or some unknown device click on it; go to update driver then ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。