【Android】Android常用的方法和类收集

本文介绍了Android中UI组件的基本使用方法,包括Button、TextView、EditText等,并展示了如何设置Activity界面特性,如全屏显示、无头部等。此外,还介绍了获取系统语言、版本号、手机型号等系统信息的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

findViewById(int id);//例如:findViewById(R.id.idTest);

setContentView(int layout);//可以当前actvity的显示视图

 

接下来是标签类,关于视图组件在多线程中,有一个规定,就是只允许创建该组件的线程修改该组件。 如果有需要,要在非创建的组件的线程中修改组件,那么可以利用Handler进行,关于详细信息可以查看我的另一篇文章 (【Android】实现线程异步的技巧标签)

标签类:

Button //按钮

 

TextView //不可编辑的文本对话框

   该类有一个属性叫做singleLine,singleLine通常搭配layout_width使用,如果多出的超过了显示的宽度,那么会以“...”显示。

 <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="end"
        android:text="你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好你好asa" />

当然也可以通过代码实现:

    public TextView createTextView(String text){
        TextView view=new TextView(this);
        view.setText(text);
        view.setSingleLine(true);
        view.setEllipsize(TruncateAt.END);
        layout.addView(view);
        return view;
    }

使用TextView可以轻松设置背景颜色,Android官方文档提供了许多的重载方法,方便开发者。但是获取背景颜色就比较麻烦了,TextView公开的方法,只能获得Drawable 对象,还需要进行如下的转化:

        Drawable drawable = textview.getBackground();
        ColorDrawable dra = (ColorDrawable) drawable;
        container.setBackgroundColor(dra.getColor());

 

EditText //可编辑的用户对话框

           该类也可以作为多行文本输入框使用,需要指定 setInputType(EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE | EditorInfo.TYPE_TEXT_FLAG_IME_MULTI_LINE);

代码实现多行文本框:

        EditText mutilText=new EditText(context);
        mutilText.setMinLines(8);
        mutilText.setInputType(EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE|EditorInfo.TYPE_TEXT_FLAG_IME_MULTI_LINE);

使用xml实现多行文本框:

    <EditText  
        android:id="@+id/add_content"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:enabled="true"  
        android:gravity="top"  
        android:inputType="textMultiLine"  
        android:minLines="8"  
        android:maxLines="10"  
        android:scrollbars="vertical"   
        android:background="@android:drawable/edit_text"  
        >  
    </EditText>  

使用EditText如果想要改变边框的话,可以通过改变background来实现,例如:

android:background="@android:drawable/bg_edittext"  

还有许多其他的样式。通过代码的话,可以调用如下方法:

editText.setBackgroundResource(R.drawable.editbox_dropdown_light_frame);

 

ScrollView //创建一个滚动条对象。如果某个对象需要滚动条,那么直接把该对象添加到该滚动对象上,再把滚动对象添加到布局中,就ok啦。

 

布局类:

RelativeLayout               //相对布局

 该类在代码中添加组件的时候,可以通过使用 RelativeLayout.LayoutParams 来指定参数,比如width,height,magin等等。

    public Button crtBtn(String text,int w,int h,int left,int top){
        Button btn=new Button(context);
        btn.setText(text);
        RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(w,h);
        lp.leftMargin=left;
        lp.topMargin=top;
        
        layout.addView(btn, lp);
        return btn;
    }

 

设置Activity界面无头部

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window win = getWindow();
        win.requestFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
    }

设置Activity全屏(通过代码)

        public void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
           //无title    
           requestWindowFeature(Window.FEATURE_NO_TITLE);    
            //全屏    
           getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,      
                          WindowManager.LayoutParams. FLAG_FULLSCREEN);   
               
            setContentView(R.layout.main);  
        }  

设置Activity全屏(通过在配置文件中设置:android:theme="@android:style/Theme.NoTitleBar.Fullscreen")

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.android.tutor"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".OpenGl_Lesson1"
                  android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="7" />
</manifest> 

 

获得系统语言

 /**
     * @return 返回当前系统语言。例如:当前设置的是“中文-中国”,则返回“zh-CN”
     */  
    public static String getSystemLanguage() {  
        return Locale.getDefault().getLanguage();  
    } 
    /**
     * @return  语言列表
     */  
    public static Locale[] getSystemLanguageList() {  
        return Locale.getAvailableLocales();  
    }  

 

获得手机版本号:

    /**
     * @return  系统版本号
     */  
    public static String getSystemVersion() {  
        return android.os.Build.VERSION.RELEASE;  
    }    

 

获得手机型号

    /**
     * @return  手机型号
     */  
    public static String getSystemModel() {  
        return android.os.Build.MODEL;  
    }  

 

获取手机厂商

    /**
     * @return  手机厂商
     */  
    public static String getDeviceBrand() {  
        return android.os.Build.BRAND;  
    }  

 

Android跳转到短信界面:

Uri smsToUri = Uri.parse("smsto:10086");
Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
// 短信内容
intent.putExtra("sms_body", "你好");
startActivity(intent);

 

Android获取屏幕的高和宽:

//在 API13之后被getSize(Point outSize)替代
int d_width=getWindowManager().getDefaultDisplay().getWidth();
int d_height=getWindowManager().getDefaultDisplay().getHeight();

//getSize(Point outSize)方法
Point outPoint=new Point();
getWindowManager().getDefaultDisplay().getSize(outPoint);
int width=outPoint.x;
int height=outPoint.y;

 

从XML 布局文件中创建 View对象

        //获得LayoutInflater服务
            LayoutInflater mInflater= (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            ViewGroup parent=...;//指定父容器
            View view= mInflater.inflate(R.layout.XML布局文件,parent,false);

 

adb shell -p 包名 -v 点击数 >电脑某一位置

自动化测试,例如:adb shell monkey -p com.android.calendar --throttle 150-v 1000000

其中-p指定包名,-v指定点次数,--throttle指定事件时间间隔,monkey就是疯狂的乱点,通过monkey可以发现许多隐藏的问题。通常点击的次数都是几万次,还可以把结果重定向文件中,方便观察。

 

adb shell input 命令

可以模拟用户的行为

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值