您的位置:首页 > 移动开发 > Android开发

a simple android user interface in java

2015-04-30 10:51 585 查看
初学者交流!

package cn.micoder.createainterfacewithjava;

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.RelativeLayout;
import android.widget.EditText;
import android.widget.Button;
import android.widget.TextView;
import android.graphics.Color;

//将DP转换成PX
import android.util.TypedValue;

//手势
import android.view.MotionEvent;
import android.view.GestureDetector;
import android.support.v4.view.GestureDetectorCompat;

//需继承接口
public class MainActivity extends ActionBarActivity implements GestureDetector.OnGestureListener,GestureDetector.OnDoubleTapListener {

private EditText myEditText;
private TextView myTextView;
private GestureDetectorCompat myGestureDector;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//创建相对布局对象
RelativeLayout myLayout = new RelativeLayout(this);
myLayout.setBackgroundColor(Color.GRAY); //need to import android.graphics.Color

//创建按钮
Button myButton = new Button(this);
myButton.setText("Click Me Bird");//设置默认显示文本
myButton.setId(1);//后面引用需要传入ID参数
//创建按钮布局参数
RelativeLayout.LayoutParams myButtonRule = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);//创建LAYOUTPARAMS对象
myButtonRule.addRule(RelativeLayout.CENTER_HORIZONTAL);//水平居中
myButtonRule.addRule(RelativeLayout.CENTER_VERTICAL);//垂直剧中

//创建编辑文本
myEditText = new EditText(this);
myEditText.setHint("Add Some Text Here!");//设置默认文本
myEditText.setWidth(convertDPtoPX(300));
//创建编辑文本布局参数
RelativeLayout.LayoutParams myEditTextRule = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
myEditTextRule.addRule(RelativeLayout.ABOVE,myButton.getId());//显示于按键之上
myEditTextRule.addRule(RelativeLayout.CENTER_HORIZONTAL);
myEditTextRule.setMargins(0,0,0,60);//设置下边距

//创建文本视图
myTextView = new TextView(this);
myTextView.setHint("Click The Button And See What Happen!");
//创建文本视图布局参数
RelativeLayout.LayoutParams myTextViewRule = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
myTextViewRule.addRule(RelativeLayout.BELOW,myButton.getId());
myTextViewRule.addRule(RelativeLayout.CENTER_HORIZONTAL);
myTextViewRule.setMargins(0,60,0,0);

//按钮单击事件监听
myButton.setOnClickListener(
//创建一个匿名内部类对象,实现View.OnClickListener接口,需import android.view.View
new View.OnClickListener() {
//需覆写抽象方法onClick(View v),处理点击事件
@Override
public void onClick(View v) {
changeTextView(getEditTextValue());
}
}
);
//按钮双击事件监听
myButton.setOnLongClickListener(
new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
clearEditText();
clearTextView();
return true;
}
}
);
//手势事件监听
myGestureDector = new GestureDetectorCompat(this,this);
myGestureDector.setOnDoubleTapListener(this);

//将部件加到布局文件中
myLayout.addView(myEditText,myEditTextRule);
myLayout.addView(myButton,myButtonRule);
myLayout.addView(myTextView,myTextViewRule);

//显示ACTIVITY
setContentView(myLayout);

}

//将DP转换成PX
public int convertDPtoPX(int dp){
return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dp,getResources().getDisplayMetrics());
}

//获取编辑文件值
private String getEditTextValue(){
return myEditText.getText().toString();
}

//改变文本视图的显示字符
private void changeTextView(String s){
myTextView.setText(s);
}

//清除EditText的值
private void clearEditText(){
myEditText.setText("");
}

//清除TextView的值
private void clearTextView(){
myTextView.setText("");
}

//实现接口里的方法
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
myTextView.setText("onSingleTapConfirmed");
return false;
}

@Override
public boolean onDoubleTap(MotionEvent e) {
myTextView.setText("onDoubleTap");
return false;
}

@Override
public boolean onDoubleTapEvent(MotionEvent e) {
myTextView.setText("onDoubleTapEvent");
return false;
}

@Override
public boolean onDown(MotionEvent e) {
myTextView.setText("onDown");
return false;
}

@Override
public void onShowPress(MotionEvent e) {
myTextView.setText("onShowPress");

}

@Override
public boolean onSingleTapUp(MotionEvent e) {
myTextView.setText("onSingleTapUp");
return false;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
myTextView.setText("onScroll");
return false;
}

@Override
public void onLongPress(MotionEvent e) {
myTextView.setText("onLongPress");
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
myTextView.setText("onFling");
return false;
}

//确认是否手势
@Override
public boolean onTouchEvent(MotionEvent event) {
myGestureDector.onTouchEvent(event);
return super.onTouchEvent(event);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息