【问题标题】:How Do Implement This In Android [closed]如何在Android中实现这个[关闭]
【发布时间】:2014-05-01 09:44:19
【问题描述】:

我是 Android 新手。谁能告诉我它是什么类型的技术?我想在密码字段中添加此功能。我可以知道实现目标的正确方法是什么吗?您能帮我提供任何代码或指向如何正确实现此目标的指南的链接吗?

【问题讨论】:

  • 带有圆形背景和相对布局的按钮。顶部是一个圆形的空视图,里面装满了代码
  • 是否有任何链接/教程? @MarcoAcierno
  • 我认为 rel 中有 10 个按钮。 rel.layout 下方的布局,带有 4 个圆形图像。然后首先单击密码,然后在第一个 rel 布局中更改第一个 btn 的图像。重复 4 次
  • 对于图像的顶部引脚,创建具有水平方向的 LinearLayout 并在其中添加四个 ImageViews。对于键,创建一个GridView,其中项目数等于12,并在getView()中返回ImageViews,返回ImageViews,其中9和11位置的透明图像......
  • @GopalRao 先生,这有什么演示/教程吗?

标签: android


【解决方案1】:

你有很多可能性来创建这种布局:

  • RelativeLayoutLinearLayout,顶部有 4 个点,底部显示您的视图,the attributes 为 toLeftOf、toRightOf 等 - 创建一个 onClickListener 方法来更改它们的状态并保存号码。
  • 多个LinearLayouts 和weight 属性填充整个空间,每个视图(四舍五入)填充空间(请参阅下面的示例以了解权重属性) - 或几个RelativeLayouts - 甚至是一个TableLayout
  • LinearLayoutGridView 下面使用ItemClickListener 方法..

根据您问题下方的cmets,有很多可能性。我用 Linear 和 RelativeLayout 选择其中之一。可能是这样的:

<!-- named activity_main.xml -->
<RelativeLayout
    ... >
    <LinearLayout
        android:id="@+id/contain"
        ... android:layout_width="250dip"
        android:orientation="horizontal"
        android:weightSum="4" >
        <!-- weightSum to 4 = whatever the screen, display 
             my children views in 4 sections -->
        <View
            ... android:layout_weight="1"
            android:background="@drawable/green_dots" />
        <!-- weight to 1 = this takes one section -->
        <View
            ... android:layout_weight="1"
            android:background="@drawable/green_dots" />
        <!-- weight to 1 = this takes one section -->
        <View
            ... android:layout_weight="1"
            android:background="@drawable/green_dots" />
        <View
            ... android:layout_weight="1"
            android:background="@drawable/green_dots" />
    </LinearLayout>
    <RelativeLayout
        android:layout_below="@id/contain" ... >
        ... Here display your buttons (or textviews) with 
            custom drawable background for each one
</RelativeLayout>  

在您的Activity 类中,它实现OnClickListener 如下:

public class MyActivity extends Activity implements OnClickListener { }

然后在这个 Activity 中的方法中:

// init your buttons var
Button one, two, three, four, five ...;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // set the layout above
    setContentView(R.layout.activity_main);
    // init your buttons
    one = (Button) findViewById(R.id.button1);
    two = (Button) findViewById(R.id.button2);
    three = (Button) findViewById(R.id.button3);
    ... etc.

    // set them to your implementation
    one.setOnClickListener(this);
    two.setOnClickListener(this);
    three.setOnClickListener(this);
    ... etc.
}

// call this function when one button is pressed
public void onClick(View view) {
    // retrieves the id of clicked button
    switch(view.getId()) {
        case R.id.button1:
             methodToSaveNumber(int);
        break;
        case R.id.button2:
             methodToSaveNumber(int);
        break;
        case R.id.button3:
             methodToSaveNumber(int);
        break;
        ... etc.
    }
}

然后在你的methodToSaveNumber 方法中:

// finally, your method to save the number of the password
public void methodToSaveNumber(int i) {
    ... do something. 
    ... change the state of the buttons, the dots, whatever you want
}  

为了向您展示它是如何工作的,可绘制的green_dots 可能是这样的:

<?xml version="1.0" encoding="utf-8"?>
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <!-- radius -->
    <corners
        android:radius="20dp" />
    <!-- border -->
    <stroke 
        android:color="@android:color/green"
         android:width="2dp" />
    <!-- background (transparent) -->
    <solid 
         android:color="#00000000" />
</shape>  

您必须告知您layouts 及其工作原理、click event and its listenerdrawables and their states(聚焦、按下、禁用……),最后,希望您能得到您想要的.

编码愉快!

【讨论】:

  • 感谢您的回答。我想添加您的方法。
  • 很高兴为您提供帮助。不确定它是否是最好的,但这可能是一个好的开始,我认为当你开始时它很容易。
  • 我认为您已经有了答案@IntelliJAmiya :) 使用 SharedPreferences,保存活动状态并在您重新打开应用程序时检索它们,在第一个活动 (A) 上。它应该有效,而且不难做到。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-27
  • 2021-02-13
  • 1970-01-01
相关资源
最近更新 更多