Android的 - 创建自定义复合构件 [英] Android - Creating Custom Compound Component

查看:129
本文介绍了Android的 - 创建自定义复合构件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始在Android的编码只是在几个星期以前,在此之前,我从来没有使用OOP(我有在C和嵌入式C语言带来的背景微控制器等),所以因为我可能会得到这个想法完全错误,但我需要帮助以继续:

I have started coding in Android just a couple of weeks ago and before that i never used oop (i have some background in c and embeded c for microcontrollers etc), so because of that i might get this idea totally wrong, but i need help to continue:

我已经创建了这完美的工作我的意图的UI使用示例活动。从那以后,我想,所以我试图做出改变,但不可能找出答案以此作为对未来设计一个自定义的化合物。该化合物具有3 textviews和2个按钮。我也实施了手势按钮旁边使用(这样我就可以向上滚动或向下更改值)。

I have created a sample activity to use as an UI which worked perfectly for my intentions. After that i wanted to use this as a custom compound for future designs so i tried to make changes but couldnt figure that out. The compound has 3 textviews and 2 buttons. i have also implemented gestures to use beside the buttons (so i can scroll up or down to change values).

现在,当我使用这个定制的复合,我得到一个错误。我想不出为什么。

Now when i use this custom compound, i get an error. I couldn't figure why.

我在这里的复合布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/CustomTouchPickerLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".CustomTouchPickerActivity" >

<TextView
    android:id="@+id/textViewMid"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textViewTop"
    android:clickable="true"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:onClick="onClickMid"
    android:paddingBottom="20sp"
    android:paddingTop="20sp"
    android:textSize="45sp" 
    android:text="Mid"
    android:layout_centerHorizontal="true"/>

<TextView
    android:id="@+id/textViewBot"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textViewMid"
    android:textSize="22sp" 
    android:text="Bot"
    android:layout_centerHorizontal="true"/>

<TextView
    android:id="@+id/textViewTop"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textViewBot"
    android:layout_marginTop="52dp"
    android:textSize="22sp" 
    android:text="Top"
    android:layout_centerHorizontal="true"/>

<Button
    android:id="@+id/buttonTop"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="OnClickButtonTop"
    android:text="Up" 
    android:layout_centerHorizontal="true"/>

<Button
    android:id="@+id/buttonBot"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textViewBot"
    android:layout_centerHorizontal="true"
    android:onClick="OnClickButtonBot"
    android:text="Down" />

这是对我复合java文件:

This is my .java file for the compound:

package com.vestel.customtouchpicker;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class CustomTouchPicker extends RelativeLayout {

public int count_sag=0;
public int count_sol=0;
public int count_button_sol=0;
public int count_button_sag=0;
private GestureDetector gestureDetector;




public CustomTouchPicker(Context context, AttributeSet attrs) {
    super(context,attrs);

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.activity_custom_touch_picker,this);

    gestureDetector = new GestureDetector(new SwipeGestureDetector());

    TextView textViewTop = (TextView)findViewById(R.id.textViewTop);
    TextView textViewBot = (TextView)findViewById(R.id.textViewBot);
    TextView textViewMid = (TextView)findViewById(R.id.textViewMid);

    Button buttonTop = (Button) findViewById(R.id.buttonTop);
    Button buttonBot = (Button) findViewById(R.id.buttonBot);

    textViewTop.setVisibility(View.INVISIBLE);
    textViewBot.setVisibility(View.INVISIBLE);

    buttonTop.setVisibility(Button.INVISIBLE);
    buttonBot.setVisibility(Button.INVISIBLE);

    textViewTop.setText("2°");
    textViewMid.setText("4°");
    textViewBot.setText("6°");

}
private class SwipeGestureDetector extends SimpleOnGestureListener {
    // Swipe properties, you can change it to make the swipe
    // longer or shorter and speed
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 200;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        try {
            float diffAbs = Math.abs(e1.getY() - e2.getY());
            float diff = e1.getX() - e2.getX();
            float diff_y = e1.getY() - e2.getY();
            float diffAbs_x = Math.abs(e1.getX() - e2.getY());

            TextView textViewTop = (TextView)findViewById(R.id.textViewTop);
            // down swipe
            if(textViewTop.getVisibility()==0){
                if (e1.getY()>e2.getY()) {
                    if(diff_y>20)
                    {
                        count_button_sol=count_button_sol+1;
                    }
                    CustomTouchPicker.this.onDownSwipe();



                }
                // up swipe
                else if (e1.getY()<e2.getY()) 
                {
                    if(-diff_y>20){
                        count_button_sol=count_button_sol+1;
                    }
                    CustomTouchPicker.this.onUpSwipe();
                }
            } 
        }catch (Exception e) {
            Log.e("YourActivity", "Error on gestures");
        }
        return false;
    }   
}
public void onClickMid(View view) 
{
    count_sol=count_sol+1;
    if(count_sol%2==1){
        TextView textViewTop = (TextView)findViewById(R.id.textViewTop);
        TextView textViewBot = (TextView)findViewById(R.id.textViewBot);            

        Button buttonTop = (Button) findViewById(R.id.buttonTop);
        Button buttonBot = (Button) findViewById(R.id.buttonBot);           

        textViewTop.setVisibility(View.VISIBLE);
        textViewBot.setVisibility(View.VISIBLE);

        buttonTop.setVisibility(Button.VISIBLE);
        buttonBot.setVisibility(Button.VISIBLE);

        textViewBot.setAlpha(0.2f);
        textViewTop.setAlpha(0.2f);
    }
    else
    {
        TextView textViewTop = (TextView)findViewById(R.id.textViewTop);
        TextView textViewBot = (TextView)findViewById(R.id.textViewBot);

        Button buttonTop = (Button) findViewById(R.id.buttonTop);
        Button buttonBot = (Button) findViewById(R.id.buttonBot);

        textViewTop.setVisibility(View.INVISIBLE);
        textViewBot.setVisibility(View.INVISIBLE);

        buttonTop.setVisibility(Button.INVISIBLE);
        buttonBot.setVisibility(Button.INVISIBLE);
    }
}
public void OnClickButtonTop (View view){

    TextView textViewTop = (TextView)findViewById(R.id.textViewTop);        
    TextView textViewBot = (TextView)findViewById(R.id.textViewBot);
    TextView textViewMid = (TextView)findViewById(R.id.textViewMid);

    switch (count_button_sol%4)
    {       
    case 0: textViewTop.setText("2°");
    textViewMid.setText("4°");
    textViewBot.setText("6°");
    break;
    case 1: textViewTop.setText("4°");
    textViewMid.setText("6°");
    textViewBot.setText("8°");
    break;
    case 2: textViewTop.setText("6°");
    textViewMid.setText("8°");
    textViewBot.setText("2°");
    break;
    case 3: textViewTop.setText("8°");
    textViewMid.setText("2°");
    textViewBot.setText("4°");
    break;
    default:textViewTop.setText("2°");
    textViewMid.setText("4°");
    textViewBot.setText("6°");
    break;
    }
    count_button_sol=count_button_sol+1;
}
public void OnClickButtonBot (View view){

    TextView textViewTop = (TextView)findViewById(R.id.textViewTop);        
    TextView textViewBot = (TextView)findViewById(R.id.textViewBot);
    TextView textViewMid = (TextView)findViewById(R.id.textViewMid);

    switch (count_button_sol%4)
    {

    case 3: textViewTop.setText("2°");
    textViewMid.setText("4°");
    textViewBot.setText("6°");
    break;
    case 2: textViewTop.setText("4°");
    textViewMid.setText("6°");
    textViewBot.setText("8°");
    break;
    case 1: textViewTop.setText("6°");
    textViewMid.setText("8°");
    textViewBot.setText("2°");
    break;
    case 0: textViewTop.setText("8°");
    textViewMid.setText("2°");
    textViewBot.setText("4°");
    break;
    default:textViewTop.setText("2°");
    textViewMid.setText("4°");
    textViewBot.setText("6°");
    break;
    }
    count_button_sol=count_button_sol+1;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (gestureDetector.onTouchEvent(event)) 
    {
        return true;
    }
    return super.onTouchEvent(event);
}


private void onDownSwipe() 
{
    TextView textViewTop = (TextView)findViewById(R.id.textViewTop);
    TextView textViewBot = (TextView)findViewById(R.id.textViewBot);
    TextView textViewMid = (TextView)findViewById(R.id.textViewMid);

    switch (count_button_sol%4){
    case 0: textViewTop.setText("2°");
    textViewMid.setText("4°");
    textViewBot.setText("6°");
    break;
    case 1: textViewTop.setText("4°");
    textViewMid.setText("6°");
    textViewBot.setText("8°");
    break;
    case 2: textViewTop.setText("6°");
    textViewMid.setText("8°");
    textViewBot.setText("2°");
    break;
    case 3: textViewTop.setText("8°");
    textViewMid.setText("2°");
    textViewBot.setText("4°");
    break;
    default:textViewTop.setText("2°");
    textViewMid.setText("4°");
    textViewBot.setText("6°");
    break;
    }
}       
private void onUpSwipe() {
    TextView textViewTop = (TextView)findViewById(R.id.textViewTop);
    //          textViewTop.setAlpha(0.5f);
    TextView textViewBot = (TextView)findViewById(R.id.textViewBot);
    TextView textViewMid = (TextView)findViewById(R.id.textViewMid);
    //          textViewBot.setAlpha(0.5f);
    switch (count_button_sol%4){
    case 3: textViewTop.setText("2°");
    textViewMid.setText("4°");
    textViewBot.setText("6°");
    break;
    case 2: textViewTop.setText("4°");
    textViewMid.setText("6°");
    textViewBot.setText("8°");
    break;
    case 1: textViewTop.setText("6°");
    textViewMid.setText("8°");
    textViewBot.setText("2°");
    break;
    case 0: textViewTop.setText("8°");
    textViewMid.setText("2°");
    textViewBot.setText("4°");
    break;
    default:textViewTop.setText("2°");
    textViewMid.setText("4°");
    textViewBot.setText("6°");
    break;

    }
}

}

这是我的测试主要活动布局

This is my main activity layout for test

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<com.vestel.customtouchpicker
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

</RelativeLayout>

和这里的MainActivity.java

And here the MainActivity.java

package com.vestel.customtouchpicker;

import android.app.Activity;
import android.os.Bundle;


public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}



}

和Android清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vestel.customtouchpicker"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.vestel.customtouchpicker.MainActivity"
        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>

</manifest>

和最后我的logcat

and lastly my logcat

    04-04 14:41:59.049: E/Trace(31339): error opening trace file: No such file or directory (2)
04-04 14:41:59.319: E/AndroidRuntime(31339): FATAL EXCEPTION: main
04-04 14:41:59.319: E/AndroidRuntime(31339): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vestel.customtouchpicker/com.vestel.customtouchpicker.MainActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class com.vestel.customtouchpicker
04-04 14:41:59.319: E/AndroidRuntime(31339):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2351)
04-04 14:41:59.319: E/AndroidRuntime(31339):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
04-04 14:41:59.319: E/AndroidRuntime(31339):    at android.app.ActivityThread.access$600(ActivityThread.java:151)
04-04 14:41:59.319: E/AndroidRuntime(31339):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331)
04-04 14:41:59.319: E/AndroidRuntime(31339):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 14:41:59.319: E/AndroidRuntime(31339):    at android.os.Looper.loop(Looper.java:155)
04-04 14:41:59.319: E/AndroidRuntime(31339):    at android.app.ActivityThread.main(ActivityThread.java:5454)
04-04 14:41:59.319: E/AndroidRuntime(31339):    at java.lang.reflect.Method.invokeNative(Native Method)
04-04 14:41:59.319: E/AndroidRuntime(31339):    at java.lang.reflect.Method.invoke(Method.java:511)
04-04 14:41:59.319: E/AndroidRuntime(31339):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
04-04 14:41:59.319: E/AndroidRuntime(31339):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
04-04 14:41:59.319: E/AndroidRuntime(31339):    at dalvik.system.NativeStart.main(Native Method)
04-04 14:41:59.319: E/AndroidRuntime(31339): Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class com.vestel.customtouchpicker
04-04 14:41:59.319: E/AndroidRuntime(31339):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)
04-04 14:41:59.319: E/AndroidRuntime(31339):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
04-04 14:41:59.319: E/AndroidRuntime(31339):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
04-04 14:41:59.319: E/AndroidRuntime(31339):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-04 14:41:59.319: E/AndroidRuntime(31339):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
04-04 14:41:59.319: E/AndroidRuntime(31339):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:363)
04-04 14:41:59.319: E/AndroidRuntime(31339):    at android.app.Activity.setContentView(Activity.java:1912)
04-04 14:41:59.319: E/AndroidRuntime(31339):    at com.vestel.customtouchpicker.MainActivity.onCreate(MainActivity.java:12)
04-04 14:41:59.319: E/AndroidRuntime(31339):    at android.app.Activity.performCreate(Activity.java:5066)
04-04 14:41:59.319: E/AndroidRuntime(31339):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
04-04 14:41:59.319: E/AndroidRuntime(31339):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
04-04 14:41:59.319: E/AndroidRuntime(31339):    ... 11 more
04-04 14:41:59.319: E/AndroidRuntime(31339): Caused by: java.lang.ClassNotFoundException: com.vestel.customtouchpicker
04-04 14:41:59.319: E/AndroidRuntime(31339):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
04-04 14:41:59.319: E/AndroidRuntime(31339):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
04-04 14:41:59.319: E/AndroidRuntime(31339):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
04-04 14:41:59.319: E/AndroidRuntime(31339):    at android.view.LayoutInflater.createView(LayoutInflater.java:552)
04-04 14:41:59.319: E/AndroidRuntime(31339):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
04-04 14:41:59.319: E/AndroidRuntime(31339):    ... 21 more

这是如果你使用codeS作为活动直接它的工作方式(当然我删除喜欢的setContentView一些线等。

by the way if you use the codes as activity directly it works (of course i removed some lines like the "setContentView" and etc

推荐答案

您缺少的主要活动布局类的名字。这是无法找到类 com.vestel.customtouchpicker ,这只是打包的自定义组件不是类名的名称。在主活动布局提供完全合格的名称为 com.vestel.customtouchpicker.CustomTouchPicker ,它应该工作。

You are missing your class name in main activity layout. It was unable to find class com.vestel.customtouchpicker, this is just package name of your custom component not class name. Give fully qualified name as com.vestel.customtouchpicker.CustomTouchPicker in your main activity layout, it should work.

更改为:

<com.vestel.customtouchpicker.CustomTouchPicker
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
/>

编辑:这是答案ClassNotFoundException的,我没有检查所有code的。因此,有可能是另一个问题。

This is answer for ClassNotFoundException, i didn't inspect all of your code. So there may be another issue.

这篇关于Android的 - 创建自定义复合构件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆